C++ Notes: Output Manipulators

Manipulators are the most common way to control output formating.

#include <iomanip>

I/O manipulators that take parameters are in the <iomanip> include file.

Default Floating-point Format

Unless you use I/O manipulators (or their equivalent), the default format for each floating-point number depends on its value.

I/O Manipulators

The following output manipulators control the format of the output stream. Include <iomanip> if you use any manipulators that have parameters. The Range column tells how long the manipulator will take effect: now inserts something at that point, next affects only the next data element, and all affects all subsequent data elements for the output stream.
Manip.RngDescription
General
endlnow Write a newline ('\n') and flush buffer.
setw(n)next Sets minimum field width on output. This sets the minimum size of the field - a larger number will use more columns. Applies only to the next element inserted in the output. Use left and right to justify the data appropriately in the field. Output is right justified by default. Equivalent to cout.width(n); To print a column of right justified numbers in a seven column field:
    cout << setw(7) << n << endl;
leftall Left justifies output in field width. Only useful after setw(n).
rightall Right justifies output in field width. Since this is the default, it is only used to override the effects of left. Only useful after setw(n).
setfill(ch)all Only useful after setw. If a value does not entirely fill a field, the character ch will be used to fill in the other characters. Default value is blank. Same effects as cout.fill(ch) For example, to print a number in a 4 character field with leading zeros (eg, 0007):
    cout << setw(4) << setfill('0') << n << endl;
For floating point values
setprecision(n)all Sets the number of digits printed to the right of the decimal point. This applies to all subsequent floating point numbers written to that output stream. However, this won't make floating-point "integers" print with a decimal point. It's necessary to use fixed for that effect. Equivalent to cout.precision(n);
fixedall Used fixed point notation for floating-point numbers. Opposite of scientific. If no precision has already been specified, it will set the precision to 6.
scientificall Formats floating-point numbers in scientific notation. Opposite of fixed.
For bool values
boolalpha
noboolalpha
all Uses alphabetic representation (true and false) for bool values. Turned off with noboolalpha.
Other
   showpoint, noshowpoint, uppercase, nouppercase, dec, oct, hex, setbase(8|10|16), showbase, noshowbase, ends, showpos, noshowpos, skipws, noskipws, ws, internal, flush, unitbuf, nounitbuf, setiosflags(f), resetiosflags(f)

Example

  const float tenth = 0.1;
  const float one   = 1.0;
  const float big   = 1234567890.0;
                          
  cout << "A. "                    << tenth << ", " << one << ", " << big << endl;
  cout << "B. " << fixed           << tenth << ", " << one << ", " << big << endl;
  cout << "C. " << scientific      << tenth << ", " << one << ", " << big << endl;
  cout << "D. " << fixed << setprecision(3) << tenth << ", " << one << ", " << big << endl;
  cout << "E. " << setprecision(20) << tenth << endl;
  cout << "F. " << setw(8) << setfill('*') << 34 << 45 << endl;
produces this output:
  A. 0.1, 1, 1.23457e+09
  B. 0.100000, 1.000000, 1234567936.000000
  C. 1.000000e-01, 1.000000e+00, 1.234568e+09
  D. 0.100, 1.000, , 1234567936.000
  E. 0.10000000149011611938
  F. ******3445