Home

Controlling and Formatting Data Output

 

The Width of Data Display

The cout class is equipped with width() to set the amount of space needed to display an item on the console. You can display the intended value with empty parentheses for width(). Here is an example:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    double PropertyValue = 782500;

    Console::Write(L"Property Value: ");
    cout.width();
    cout << PropertyValue;

    Console::WriteLine();
    return 0;
}

This would produce:

Property Value: 782500
Press any key to continue . . .

As an alternative, you can type an integer in the parentheses of width(). In order to display the intended value that could be a string, an integer or a floating number, the compiler would count the number of characters (for a char or a string) or the number of digits (for an integer or a float, including the period for decimals). If the value of the argument you supplied is less than or equal to the number of characters or digits, the compiler would ignore the number in the parentheses and display the intended value. If the value in the parentheses is greater than the number of characters or digits, the compiler would display the intended value using the total value in the parentheses but leaving empty spaces on the left of the intended value.

The value to display comes after calling width(). If you are displaying only one value, the scenario above would apply straight forward. Here is an example:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    double PropertyValue = 782500;

    Console::Write(L"Property Value: ");
    cout.width(16);
    cout << PropertyValue;

    Console::WriteLine();
    return 0;
}

This would produce:

Property Value:           782500
Press any key to continue . . .

To display various values, you can call width() for each and they can use different width values as in the following example:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    double PropertyValue1 = 782500,
	   PropertyValue2 = 625560,
	   PropertyValue3 = 284782;

    Console::Write(L"Property Value 1: ");
    cout.width(8);
    cout << PropertyValue1 << endl;
    Console::Write(L"Property Value 2: ");
    cout.width(14);
    cout << PropertyValue2 << endl;
    Console::Write(L"Property Value 3: ");
    cout.width(10);
    cout << PropertyValue3 << endl;

    Console::WriteLine();
    return 0;
}

This would produce:

Property Value 1:   782500
Property Value 2:         625560
Property Value 3:     284782

Press any key to continue . . .
 

Filling the Empty Space

As we have seen already, the compiler leaves empty spaces on the left side of the displaying value if the width is greater than the needed space. If you do not want that space to be empty, you can fill it with any character you want. To do that, call fill(). You can use it with empty parentheses. Here is an example:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    double PropertyValue1 = 782500,
		   PropertyValue2 = 625560,
		   PropertyValue3 = 284782;

    Console::Write(L"Property Value 1: ");
    cout.width(8);
    cout.fill();
    cout << PropertyValue1 << endl;
    Console::Write(L"Property Value 2: ");
    cout.width(14);
    cout << PropertyValue2 << endl;
    Console::Write(L"Property Value 3: ");
    cout.width(10);
    cout << PropertyValue3 << endl;

    Console::WriteLine();
    return 0;
}

Alternatively, you can enter in its parentheses the character that would be used in place of the empty spaces. The  character can be included between single-quotes. If no fill() was called previously, it displays the intended value "as is". If there was a previous call to fill(), the fill() immediately following would still keep the value in the parentheses of the previous fill(), then empty it. In other words, if there is another call to fill() without a value in its parentheses, no character would be displayed, until a new call to the fill(). Here is an example:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    double PropertyValue1 = 782500,
	   PropertyValue2 = 625560,
	   PropertyValue3 = 284782;

    Console::Write(L"Property Value 1: ");
    cout.width(8);
    cout.fill();
    cout << PropertyValue1 << endl;
    Console::Write(L"Property Value 2: ");
    cout.width(14);
    cout.fill('#');
    cout << PropertyValue2 << endl;
    Console::Write(L"Property Value 3: ");
    cout.width(10);
    cout.fill();
    cout << PropertyValue3 << endl;

    Console::WriteLine();
    return 0;
}

This would produce:

Property Value 1:   782500
Property Value 2: ########625560
Property Value 3: ####284782

Press any key to continue . . .
 

The dec, hex, and oct Operators

The most usual way to show an integer number on the console is to display it “as is”. To display a variable that has been declared and initialized as

int Value = 152;

you would write:

cout << Value;

The compiler would take care of displaying it. If you have an integer in decimal format and would like to display its equivalent hexadecimal representation, use the hex operator as follows:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    int Value = 242;

    cout << "Value = " << Value << endl;
    cout << "Value = " << hex << Value << endl;

    Console::WriteLine();
    return 0;
}

This would produce:

Value = 242
Value = f2

Press any key to continue . . .

If you have a hexadecimal integer and would like to display it in a decimal format, simply use the cout class followed by the value. Here is an example:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    cout << "Number = " << 0xFE28D;

    Console::WriteLine();
    return 0;
}

This would produce:

Number = 1041037
Press any key to continue . . .

Although all integers are displayed in decimal format by default, you can explicitly ask the compiler to reinforce that notation. You can also display the same value in either format as follows:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    int Variable = 28045;

    cout << "Declared Variable: " << Variable << endl;
    cout << "In decimal: " << dec << Variable << endl;
    cout << "In hexadecimal: " << hex << Variable << endl;

    Console::WriteLine();
    return 0;
}

This would produce:

Declared Variable: 28045
In decimal: 28045
In hexadecimal: 6d8d

Press any key to continue...
 

The uppercase Attribute

As you have found out, the hex attribute is used to display the hexadecimal equivalent of a decimal number, but such a number displays in lowercase. To display the hexadecimal letters to uppercase, include the uppercase attribute. Here is an example:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    int Value = 242642;

    cout << "Value = " << Value << endl;
    cout << "Value = " << hex << uppercase << Value << endl;

    Console::WriteLine();
    return 0;
}

This would produce:

Value = 242642
Value = 3B3D2

Press any key to continue . . .

The uppercase attribute can precede or succeed the hex attribute.

Setting the I/O Flag

The width() is used to right-align the value(s) displayed that otherwise would be left aligned. If you want to explicitly left align a value, you can use setiosflags(). To use setiosflags(), you must qualify its flags using the library in which the flag is defined. setiosflags is defined in the iomanip.h header file of the std namespace.

The ios::left flag is used to left-align a value. Here is a simple example that displays an integer:

#include <iostream>
#include <iomanip>

using namespace std;
using namespace System;

int main()
{
    double PropertyValue = 288015;

    Console::Write(L"Property Value: ");
    cout << setiosflags(ios::left) << PropertyValue;

    Console::WriteLine();
    return 0;
}

This would produce:

Property Value: 288015
Press any key to continue . . .

After using setiosflags(), any value displayed would follow its rules. To remove the formatting set by setiosflags(), use resetiosflags(), usually with the same argument. Here is an example:

#include <iostream>
#include <iomanip>

using namespace std;
using namespace System;

int main()
{
    	double PropertyValue = 288015;
	int    YearBuilt     = 1966;

	Console::Write(L"Property Value: ");
    	cout << setiosflags(ios::left) << PropertyValue;
	Console::WriteLine();

	Console::Write(L"Year Buil:     ");
	cout << resetiosflags(ios::left) << setw(8) << YearBuilt;

	Console::WriteLine();
	return 0;
}

This would produce:

Property Value: 288015
Year Buil:         1966
Press any key to continue . . .

Formatting Floating-Point Numbers in C++

In most programs that use precise numbers, you need to be able to format your values to display appropriately. This is made possible with the use of setprecision(). This takes one value in its parentheses as the number of decimal places displayed after the period of the floating number. In order to effectively use setprecision(), first call setiosflags() with the appropriate flag. The ios::fixed flag allows you to specify the exact number of decimal values used in setprecision(). Here is an example:

#include <iostream>
#include <iomanip>

using namespace std;
using namespace System;

int main()
{
    	double PropertyValue = 265500;

	Console::Write(L"Property Value: ");
	cout << setiosflags(ios::fixed) << setprecision(2)
		 << PropertyValue << endl;

	Console::WriteLine();
	return 0;
}

This would produce:

Property Value: 265500.00

Press any key to continue . . .

Since setiosflags() and setprecision() apply their formatting to all values set after they have been called, you can use them once and format your other values. That is, until you call them again. You can also display floating numbers using a scientific format. To do that, set the flag of setiosflags() to ios::scientific. Here is an example:

#include <iostream>
#include <iomanip>

using namespace std;
using namespace System;

int main()
{
    double Pop = 25.44059,
           Distance = 212.625,
           MaxExp = 709.78222656;

	Console::Write(L"Population: ");
	cout << setiosflags(ios::scientific) << setprecision(2);
 	cout << Pop << endl;
	Console::Write("Distance: ");
	cout << Distance << endl;
	Console::Write("Exponent: ");
	cout << MaxExp << endl;

	Console::WriteLine();
	return 0;
}

This would produce:

Population: 2.54e+001
Distance: 2.13e+002
Exponent: 7.10e+002

Press any key to continue . . .

C How to Display Data

The C language is the parent of C++. As such, C++ inherits what is available from C. This flexibility also allows you to mix the way things are done in C and the features that are available in C++. Although C is C++’ parent, you don't need to know (anything) about C in order to program in C++. Nevertheless, we will review these features transparently and we will be concerned only with the reason we are here: how to program in C++.

To format the display of data in C++, the cout class is complete. In some circumstances, you will need to know how other objects format data. The most common way to display data in C, and some legacy C++ code, is with printf(). As flexible as it is, it doesn't use the type of syntax we use in C++.

To display data, everything depends on how you want it to appear. Data types are divided in categories: characters, integers, floating-point numbers, and strings. To display a particular type, you type the percent operator “%”, followed by the category of data. An example would be “%d”. To display a character, use the %c format to represent a character variable. Here is an example:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    char PropertyType = 'S';

    printf("Property Type: %c", PropertyType);

    Console::WriteLine();
    return 0;
}

To display an integer, use the %operator with one of the following tags:

Tag Integer Type to Display
d signed decimal integer
i signed decimal integer
o unsigned octal integer
u unsigned decimal integer
x unsigned hexadecimal int (with a, b, c, d, e, f)
X unsigned hexadecimal int (with A, B, C, D, E, F)

To display a floating-point number, use the % operator with one of the following formats:

 

Tag To display
f signed value of the form [-]dddd.dddd
e signed value of the form [-]d.dddd or e[+/-]ddd
g signed value in either e or f form, based on given value and precision.
Trailing zeros and the decimal point are printed if necessary.
E Same as e; with E for exponent
G Same as g; with E for exponent if e format used

To set the number of decimal places after the period, type the % operator, followed by a period, followed by an integer for the number of decimal places desired, followed by the desired tag. Here is an example:

#include <iostream>

using namespace std;
using namespace System;

int main()
{
    double Floater = 3255.01265;

    printf("Floater: %.3f", Floater);
    
    return 0;
}

To display a string, use the %s format. 

 

Previous Copyright © 2006-2016, FunctionX, Inc. Next