Formatting a programs output

In comparison to other computer languages modifying the looks of numbers appearing in your print outs is somewhat more counter-intuitive in C++ than in other languages. In addition the type of data you wish to format ( integer vs. floating point ) plays a role. In any case, whenever you wish to format your output you have to include a new header file :

#include <iomanip>

The output formatting takes place by calling upon certain objects which are called stream manipulators which - when called upon - change some parts of the objects performing the data output for you.

Total number of spaces

cout << setw ( 12) ;

will provide 12 spaces for the next item to be printed, whether that is an integer number or floating point number or character string. The number will appear right-justified meaning that if the number to be printed is less than 12 characters long leading blank spaces will be provided. This comes in handy if you want to generate a table of numbers of different lengths.
Unfortunately, setw() effects only the next item to be printed and therefore setw() has to be called upon many times, depending on the make-up of your program. If you don't issue a call to setw() only the minimum amount of space will be provided.

#include <iostream>
#include <iomanip>

using namespace std ;

int main ()
{
     int i = 13 , j= 500 ;

     cout << setw(12) << i << endl << j << endl ;
}
Produces the output :
          13
500
which is probably not what you hoped for. But

cout << setw(12) << i << endl < < setw(12) << j << endl ;

would do the trick producing :

          13
         500

Set precision (for floating point numbers only)

cout << setprecision ( 9 ) ;

allows you to specify the number of significant digits. Fortunately, the effect of setprecision affects every output for the entire time of your program's execution. The precision can be changed - if you so desire - by a second call to setprecision.

#include <iostream>
#include <iomanip>
#include <cmath>

int main ()
{
        double pi , pi4 ;

        pi4 = atan ( 1. ) ;
        cout << "pi/4 = " << setprecision ( 12 ) << pi4 << endl ;
        pi  = 4.*pi4 ;
        cout << "pi   = " << pi << endl ;

        cout << setiosflags ( ios:: scientific ) ; <---- see "Mode of display" below
        cout << "pi/4 = " << pi4 << endl ;
        cout << "pi   = " << pi  << endl ;
}

produces the following output :

pi/4 = 0.785398163397
pi   = 3.14159265359
pi/4 = 7.853981633974e-01
pi   = 3.141592653590e+00

Set mode of display (for floating point numbers only)

The number 123.4 can be displayed in the way just written or as 1.234e+2, the first mode being called "fixed", the second being called "scientific". You can force your program to print numbers in either mode by :

cout << setiosflags (ios::fixed) ;

or

cout << setiosflags (ios::scientific) ;

You can call setiosflags() in your program as often as you like and with that toggle back and forth between the fixed and scientific mode as you see fit. The effect of setiosflags() lasts until the next call to setiosflags() produces changes to the subsequent output.

Last revised: 08/23/13