Formatting the Output

To format a program's output means to control how it appears visually on the screen or on a printer. We use the special ostream variable cout together with the insertion operator << to accomplish this. Here is what an output statement looks like.
cout << ExpressionOrManipulator <<  ExpressionOrManipulator <<  ... ;
The C++ standard library supplies many manipulators, here are a few of them:
Category Manipulator Description
flow endl
  • Inserts an end-of-line character '\n': the next output will be at the start of the next line.
  • Flushes output stream possibly allowing output to be seen sooner than with '\n' alone in certain situations.
Field Width setw(n)
  • Controls how many character positions the next data item should occupy when it is output.
  • Not sticky: Only one item is affected.
  • n: an integer that represents the field width (number of character positions).
  • The output is right justified by default.
left
  • Sticky: all subsequent setw(n) will align left until right is used
right
  • Sticky: all subsequent setw(n) will align right until left is used
Floating Point setprecision(n)
  • Sticky: set a new precision - exact meaning of precision varies
  • By default this controls the maximum number of digits to use when printing floating point numbers.
  • If the number is too large or small for the precision to include the decimal, then output will switch to scientific notation.
  • Default precision is 6.
fixed
  • Sticky: switch floating point output to use fixed point notation,
  • setprecision(n) now controls the number of places that appear after the decimal.
scientific
  • Sticky: switch floating point output to in scientific notation,
  • setprecision(n) controls the maximum number of significant figures.
defaultfloat
  • Sticky: switch floating point output back to the default.
    Does not reset setprecision(n) or showpoint.
showpoint
  • Sticky: Forces the printing of all significant figures as indicated by setprecision(n), even if they are trailing zeros.
noshowpoint
  • Sticky: disables showpoint
In this table, the manipulators without arguments (such as endl, fixed, showpoint) are available through the header file iostream.
Those with argument (such as setw(n), setprecision(n)) require the header file iomanip.

Now look at an example and see how those manipulators are used:


#include <iostream>
#include <iomanip>
using namespace std;

int main() {
  //Constant to control column width in table
  const int colw = 11;

  //Set precision to 3 instead of the default of 6
  cout << setprecision(3);

  //Align column labels left
  cout << left;

  //Print column labels
  cout << setw(colw) << "Default" << "|"
       << setw(colw) << "Fixed" << "|" 
       << setw(colw) << "Scientific" << "|"
       << setw(colw) << "+Showpoint" << "|"
       << setw(colw) << "Def. Float" << "|"
       << setw(colw) << "-Showpoint" << endl;

  //Print a separator line below the labels for 6 column widths + 5 '|'
  //setfill(char) prints the char specified instead of a ' ' for wide columns
  cout << setfill('-') << setw(colw*6 + 5) << "" << setfill(' ') << endl;

  //Align rest of table right
  cout << right;

  //Print row 1
  float f = 3;
  cout                 << setw(colw) << f << "|"
       << fixed        << setw(colw) << f << "|" 
       << scientific   << setw(colw) << f << "|"
       << showpoint    << setw(colw) << f << "|"
       << defaultfloat << setw(colw) << f << "|"
       << noshowpoint  << setw(colw) << f <<  endl;
 
  //Print row 2
  f = 123456.78;
  cout                 << setw(colw) << f << "|"
       << fixed        << setw(colw) << f << "|" 
       << scientific   << setw(colw) << f << "|"
       << showpoint    << setw(colw) << f << "|"
       << defaultfloat << setw(colw) << f << "|"
       << noshowpoint  << setw(colw) << f <<  endl;

  //Print row 3
  f = .000123;
  cout                 << setw(colw) << f << "|"
       << fixed        << setw(colw) << f << "|" 
       << scientific   << setw(colw) << f << "|"
       << showpoint    << setw(colw) << f << "|"
       << defaultfloat << setw(colw) << f << "|"
       << noshowpoint  << setw(colw) << f <<  endl;

  return 0;
}
You may run this program and compare the output with what you expected.



Copyright: Department of Computer Science, University of Regina.