The goal of this handout is to show how to accomplish formatting for C++ stream output.
The information is organized by the category or type of action you wish to do and then by the options or choices within a category. Each example uses cout for simplicity but any other text output stream could be used. The notes contains information which should not be ignored! In particular, note that all operations except setw are persistant, that is, they remain in effect until changed. The fieldwidth operation setw must be invoked each time it is desired!
Category of Action | Options or Choices | Examples | Notes |
Set the minimum number of characters to be output: the fieldwidth.
If the item to output does not require the amount of characters specified then a fill character will be used to fill the empty space and the justification will determine how this is done. If the item to output needs more characters than the minimum fieldwidth then the needed characters will be output. | Specify an integer for the minimum fieldwidth | cout << setw(12); | The action of setw only applies to the next item to be sent to the output stream.
Immediately after an item is output, the minimum fieldwidth is set back to zero. Thus, to maintain control of the fieldwidth, you must use setw before every output operation. |
Justification | Left justification
Right justification Internal justification | cout << left;
cout << right; cout << internal; |
The default is left. This is natural for strings but is not as good for numbers. Use right for numbers if alignment in columns is important. Use internal to put fill characters between the sign or the number base symbol and the numerical value. |
Fill Character | Specify a character | cout << setfill('*'); cout << setfill(' '); | After using a special fill character be sure to reset to a blank |
Integer/Pointer Format | Decimal
Octal Hexadecimal Automatic | cout << dec;
cout << oct; cout << hex; cout.unsetf
|
The default is automatic, that is, let the stream decide based on the type.
To return the setting to automatic is cumbersome! |
Floating Format | Fixed
Scientific Automatic |
cout << fixed;
cout << scientific; cout.unsetf
| The default is automatic, that is, let the stream decide based on the size of the number.
To return the setting to automatic is cumbersome! |
Floating Precision | Specify an integer for the precision | cout << setprecision(8);
To reset to default use: cout << setprecision(6); | The default precision is 6.
If fixed format is active then the precision is the number of digits after the decimal point. If scientific format is active then the precision is the total number of digits in the main value excluding the exponent field. |
Show / Hide Options | Show/hide the octal or hex prefix characters
Show/hide decimal point Show/hide leading + sign | cout << showbase; cout << noshowbase; cout << showpoint;
cout << showpos;
| The default options are
no .
The decimal point will normally be shown if it is needed and omitted if not. |