Next: Example Program: Tabulation of Up: Further Assignment Statements & Previous: Specialised Assignment Statements

Formatting of output

When considering output in 6.5 no consideration was given to the format of the output produced. It was assumed that all output would be formatted using the default settings. The default settings print integers using as many characters as are required and real values are printed with up to six decimal digits (some compilers give six digits after the decimal point).

Consider the following portion of C++. This portion uses a construct not covered yet, namely, a for statement. The for statement is used for implementing loops and will be covered later. The statement starting with for has the effect of executing the statements between the braces {} as i takes the values 1, 2, 3 and 4.

for (i=1; i<5; i++)
  {
   cout << "Enter an integer value: ";
   cin >> x;
   cout << x << "   " << sqrt(x) << endl;
  }
then output as follows might be produced:
1   1
5   2.23607
1678   40.9634
36   6
This is very untidy and difficult to read, it would be preferable if it could appear as follows:
    1   1.00000
    5   2.23607
 1678  40.96340
   36   6.00000
with the least significant digits of the integers aligned and the decimal points in the real numbers aligned. It is possible to achieve this degree of control on the output format by using output manipulators.

Before looking at manipulators scientific notation for the display of floating point numbers is considered. Scientific notation allows very large or very small numbers to be written in a more convenient form. Thus a number like 67453000000000000 is better written as $\displaystyle{6.7453\times10^{16}}$ and a number like 0.0000000000001245 is better written as $\displaystyle{1.245\times10^{-13}}$. C++ allows this type of notation by replacing the `ten to the power of' by e or E. Thus the above numbers could be written in C++ as 6.7453e16 and 1.245e-13. These forms can be used in writing constants in C++ and in input and output. On output, if a number is too large to display in six digits then scientific notation will be used by default. For example 12345678.34 might be output as 1.23457e+07.

The first manipulator considered is setiosflags, this allows the output of floating point numbers to be
fixedfixed format i.e. no scientific notation
scientificscientific notation
showpoint displays decimal point and trailing zeros
The flags that are to be set are specified in the setiosflags manipulator as follows:

setiosflags(ios::flagname)
If more than one flag is to be set then another ios::flagname can be included, separated by a | from the other setting in the above call. Thus the following output statement would set fixed format with the decimal point displayed:
cout << setiosflags(ios::fixed | ios::showpoint);

This would ensure that a number like 1.0 would be displayed as 1.0 rather than as 1. These flags remain in effect until explicitly changed.

Another useful manipulator is the setprecision manipulator, this takes one parameter which indicates the number of decimal places of accuracy to be printed. This accuracy remains in effect until it is reset. The setprecision manipulator may be used when none of the iosflags have been set. However there is some confusion over what constitutes precision, some compilers will produce n digits in total and others n digits after the point when setprecision(n) is used on its own. However if it is used after the flags fixed or scientific have been set it will produce n digits after the decimal point.

For the moment the most suitable setting of the iosflags for output are fixed and showpoint.

The following portion of C++

float x, y;
x = 12.2345,
y = 1.0;
cout << setiosflags(ios::fixed | ios::showpoint)
     << setprecision(2);
cout << x << endl
     << y << endl;
would output
12.23
1.00
that is, in fixed format with two places after the point and the point displayed. Without the ios flag set to showpoint y would have been printed as 1. If the decimal points have to be aligned then the field width has to be set. This is done using the setw manipulator which takes a single parameter which indicates the width of field in which the output value is to be placed. The value is placed right-justified in the field. The field width remains in effect only for the next data item displayed. Thus if the lines:
cout << setw(7) << x << endl
     << setw(7) << y << endl;
were added to the above portion of code the output would be:
12.23
1.00
  12.23
   1.00
Note 1: The file iomanip.h must be included if the above manipulators are to be used. There are many more facilities available by using input/output manipulators but the above is enough to allow the writing of programs that produce sensible formatting of output in most cases.

Note 2: The output width is reset to the default after every variable is output, so that it was necessary to use setw(7) twice, once before each variable that was output.



Next: Example Program: Tabulation of Up: Further Assignment Statements & Previous: Specialised Assignment Statements