Formatting Output: iostream manipulators

To format a program's output means to control how it appears visually on the screen. C++ uses format flags to define the format used when values are read and written.

A flag is a Boolean variable whose value indicates whether the flag is set (1) or cleared (0). For example, if the boolalpha flag is set, Boolean values are input and output as true and false. If the flag is not set (the default), 1 and 0 are input output.

I/O stream manipulators are used to set and clear format flags. For example, you have been using the endl manipulator to terminate an output line. endl is an example of a manipulator that does not take an argument. By contrast setw(val) is a manipulator that takes an argument, and sets the field width for printing a value to val.

Here is a list of useful stream manipulators:

setprecision(val) Set val as the new precision (i.e., number of significant digits)
setw(val) The next field width is val (setw does not persist beyond the next output operation)
showpoint Always write a decimal point
fixed Use decimal notation
scientific Use E-notation
setfill(char) Use char as the fill character
boolalpha Use textual representation for boolean literals (true, false)
noboolalpha Use numeric representation for boolean literals (1, 0)
flush Flush the output buffer to its device
endl Insert a newline ('\n') into the output buffer, and flush the buffer
setiosflags(flags) Sets all format flags identified by flags
resetiosflags(mask) Clears all flags of the group identified by mask


Floating-Point Output

Output of floats is controlled by a format and a precision:

  cout << 1234.56789;
default ("best of") 1234.57
scientific 1.234568e+03
fixed 1234.567890
   

Example

iomanip.cpp

What is Scientific Format?

Interpreting very large or very small decimal numbers (i.e., fixed format) is a difficult task. For example, which of the following numbers is larger? 1000000000000000000000000 or 900000000000000000000000

In order to answer this, you are forced to count the number of digits in each (there are 24 0s in the first and only 23 in the second) to find that the first number is larger.

Very large and very small numbers can be more clearly represented in scientific notation. In scientific notation, a number is represented as a value times a power of 10. For example, the first number is (1 * 1024), while the second is (9 * 1023). Using this notation, it is immediately clear that the first number is larger than the second, simply by looking at the exponents.

C++ utilizes an even more abbreviated shorthand for scientific notation. Instead of writing (X * 10Y), C++ simply writes XeY, with the 'e' interpreted as "times 10 to the power of ...".

Example: in scientific format, 0.0000009999 is printed as 9.999e-7.


Currency Format

Printing in currency format ("$n.nn") can be achieved using three manipulators: fixed, showpoint, and setprecision(2):
  float x = 123.456789, y = 123.0;
  cout << "currency:  "
       << fixed << showpoint << setprecision(2)
       << "$" << x
       << endl
       << '$' << y
       << endl;

Example

iomanip.cpp


I/O for Boolean Values: boolalpha

By default Boolean values are represented using numbers, 0 for false and 1 for true:

   cout << true; // prints 1
   bool bvar; cin >> bvar;  // user must enter 0 or 1 (otherwise an error)

It is sometimes convenient to print out these values as words. The boolalpha flag defines the format used to read or to write Boolean values:

Special manipulators are available to manipulate this flag: the boolalpha manipulator will print out true and false values as true and false respectively. The noboolalpha manipulator will return the printed values to 1 and 0.

Example

boolalpha.cpp