Formatted output to your monitor or to a file, from your program is important, especially when the presentation of your results is more easily understood by your user with some form of organization. For example, if your program tabulates a loan payment program by monthly payments, including a new principal amount, interest paid, payment amount, and the number of the payment, it would aid the user of the program if these values were presented in consistent, aligned columns. Further, such a program deals with dollar amounts, so consistently showing two decimal points of precision, even if this means displaying trailing zeroes, is preferable.
C++ provides for formated output for these situations, via manipulators. You have
used some of these manipulators, like endl
, already. Others, like
setprecision()
or fixed>
are presented here. Besides
instructing the preprocessor to include the iostream header file, you will
need to include the iomanip header file for the manipulators that require
parameters, like setfill(). Include iomanip at the top of your source file
like this:
#include <iomanip>
The setprecision(n)
manipulator does what its name implies. It sets
the maximum number of decimal places displayed by cout to the parameter following
its call in parentheses. Since it is used as a manipulator with cout
and
the stream insertion operator, it must be used like this:
cout << setprecision(2);
Since the value within the parentheses is 2, the output following this statement will
display at most two decimal places, whether or not this value is rounded from several
decimal places of precision. Don't forget to include iomamip
when using
setprecision().
The fixed
manipulator sets the output of the cout statements following it in your
program to fixed decimal format. It will force cout to display the six default decimal
places specified in cout, or another number of places, specified in your code. Use it in
your code like this:
cout << fixed;
The scientific
manipulator forces cout to display expressions in scientific notation.
So a number like 354.79 would be displayed as 3.547900e+002. Like fixed, scientific
has no parameter and does not require iomanip
. It will look like this in your code:
cout << scientific;
The showpoint
manipulator forces cout to display the decimal point and trailing
zeroes, whether the value returned by the expression is sufficiently precise or not. This
is useful for keeping columns aligned and representing data uniformly. It will look like
this in your source code:
cout << showpoint;
Often showpoint
is used in conjunction with the fixed
manipulator. This
collaboration will result in a set representation of a group of values. So combining these
two with, for instance, setprecision(2)
will do nicley to display a column or several
columns of dollar amounts, like the following example demonstrates.
cout << fixed << showpoint; cout << setprecision(2); for(i=1; i<=n; i++) cout << "\tPayment " << i << " reduces principal amount to: " << principal << endl;
The setw(n)
manipulator sets the width of the field in which the next expression
is displayed. Unlike setprecision()
, which must be unset, setw()
applies only to the next field. So to specify a column width in which to print a value,
setw(n)
, where n is the number of spaces dedicated to the value, within the line,
is the best choice. In your code it will look like this:
cout << setw(5) << x;
Extra spaces generated by the setw(n)
mamipulator, not taken by the expression,
are, by default, displayed as spaces to the left of the right-justified expression. In most
cases, this is acceptable. We can use the ostream manipulator setfill(ch)
to
fill this space with an alternative character to the space. The character value held in
ch, be it '#', '$', '&', '-', or any other printable ASCII character, even letters
or numbers, will then be used in lieu of the space, ' ', to fill the excess space in the
field. When combined with setw(n)
in your code, it will look like this:
cout << setfill('%'); cout << setw(10) << x;
The left
manipulator justifies a width adjusted expression to the left of the
field. In essence it left justifies everything following it until it is unset or overridden.
Since it has no parameter, it is part of the iostream
header file, and is an
ostream variable manipulator. It will look like this in your code:
cout << left; cout << setfill('%'); cout << setw(10) << x;
As you might guess, right
behaves like left
. In the above example,
x will be printed on the left side of the field, with '%' filling in the
spaces to the right. If left
were changed to right
, the value would
be printed to the right of the established field with the percent signs to the left.
To undo changes made to the default print scheme use unsetf()
. It's syntax is
shown below:
cout.unsetf(ios::left);