Formatted Output with iostream.h
Introduction
cout (the standard output stream) can be formatted using manipulators and member functions which are available in iomanip.h. There is a discussion of many of these in Chapter 11 of Deitel & Deitel, C++. It's always helpful to have some additional examples, and that's what this handout is about.
Annotated Examples
The C++ code below shows the use of a number of cout formatting manipulators and functions. For information on all the available options, see Borland Turbo C help under ios class summary.
| Topic | Use | See Lines | Note |
|---|---|---|---|
| setting field width | setw() manipulator |
12-24 |
1 |
| set the base of the output values | setbase() manipulator |
18-24 |
1 |
| Setting value precision | cout.precision() function |
95-101 |
- |
| setprecision() manipulator |
33-38 |
1 |
|
| Filling output fields | setfill() manipulator |
26-31 |
1 |
| cout.fill() function |
85-93 |
- |
|
| left justifying output | setiosflags(ios::left) function |
43-50 |
- |
| right justifying output | setiosflags(ios::right) function |
52-59 |
- |
| put + signs in front of positive values | setiosflags(ios::showpos) |
61-66 |
- |
| show decimal point in floating point values | setiosflags(ios::showpoint) |
68-73 |
- |
| use scientific notation for floating point values | setiosflags(ios::scientific) |
75-80 |
- |
| reset a setiosflag feature | resetiosflags() |
50 |
- |
1. This specifier must be called for each output operation (that is, for every value output).
Examples
1
2 #include <iostream.h>
3 #include <iomanip.h>
4 int main() {
5 float x1 = 56.5, x2 = 45.875, x3 = 33.0;
6 int i1 = 11, i= 43, i3 = 77;
7
8 /***************************************************************
9
10 // examples using setw, setbase, setfill and setprecision manipulators
11
12 // set field width to 8 for each of the variables
13 // the following outputs the values in fields 8 wide
14 // the following outputs: 56.6 45.875 33
15
16 cout << setw(8) << x1 << setw(8) << x2 << setw(8) << x3 << endl;
17
18 // set output base (may only use 8 10 or 16,
19 // a zero indicates current base
20 // the following outputs: 400 100 256
21
22 cout << setw(8) << setbase(8) << 256
23 << setw(8) << setbase(16) << 256
24 << setw(8) << setbase(0) << 256 << endl;
25
26 // set leading fill characters
27 // the following outputs *****25600000256 256
28
29 cout << setw(8) << setfill('*') << 256
30 << setw(8) << setfill('0') << 256
31 << setw(8) << setfill(' ') << 256 << endl;
32
33 // set floating point precision
34 // the following outputs: 3.14 3.142 3.1416
35
36 cout << setw(8) << setprecision(2) << 3.14159
37 << setw(8) << setprecision(3) << 3.14159
38 << setw(8) << setprecision(4) << 3.14159 << endl;
39
40 //**************************************************************
41 // examples using setiosflags
42
43 // left justify values in output fields
44 // the following outputs: 256 256 256
45
46 cout << setiosflags(ios::left);
47 cout << setw(8) << 256
48 << setw(8) << 256
49 << setw(8) << 256 << endl;
50 cout << resetiosflags(ios::left);
51
52 // right justify values in output fields
53 // the following outputs: 256 256 256
54
55 cout << setiosflags(ios::right);
56 cout << setw(8) << 256
57 << setw(8) << 256
58 << setw(8) << 256 << endl;
59 cout << resetiosflags(ios::right);
60
61 // add + to positive values
62 // the following outputs: +256
63
64 cout << setiosflags(ios::showpos);
65 cout << setw(8) << 256<< endl;
66 cout << resetiosflags(ios::showpos);
67
68 // force decimal point in floating point output
69 // the following outputs: 256.0000
70
71 cout << setiosflags(ios::showpoint);
72 cout << setw(8) << 256.0 << endl;
73 cout << resetiosflags(ios::showpoint);
74
75 // use scientific notation
76 // the following outputs: 2.56e+02
77
78 cout << setiosflags(ios::scientific);
79 cout << setw(8) << 256.0 << endl;
80 cout << resetiosflags(ios::scientific);
81
82 /****************************************************************
83 // examples using cout member functions
84
85 // set leading fill characters
86 // the following outputs *****25600000256 256
87
88 cout.fill('*');
89 cout << setw(8) << 256 ;
90 cout.fill('0');
91 cout << setw(8) << 256;
92 cout.fill(' ');
93 cout << setw(8) << 256 << endl;
94
95 // set precision to three decimal places
96 // the following outputs 2.222 3.333 4.444
97
98 cout.precision(3);
99 cout << setw(8) << 2.2222;
100 cout << setw(8) << 3.3333;
101 cout << setw(8) << 4.4444<< endl;
102
103 }