Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
C++ How to Program, 5/e

ISBN:
0-13-185757-6
© 2005
pages: 1500
Buy the Book!
Amazon logo
InformIT logo

This tutorial introduces ANSI/ISO C++'s stream formatting capabilties, including stream manipulators, justification, padding, integer formats, floating-point number formats, uppercase/lowercase control, formatting booleans as strings, and setting and restoring stream format states. This tutorial is intented for students and developers who are familiar with basic C++ input and output techniques using cin and cout. The techniques shown here can also be applied to file-based streams or other streams that extend the standard C++ stream class hierarchy.

Download the code examples for this tutorial.

[Note: This tutorial is an excerpt (Section 15.7) of Chapter 15, Stream Input/Output, from our textbook C++ How to Program, 5/e. These tutorials may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., C++ HOW TO PROGRAM, ©2005, pp.787-797. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

15.7 Stream Format States and Stream Manipulators (Continued)

15.7.3 Padding (fill, setfill)

The fill member function specifies the fill character to be used with justified fields; if no value is specified, spaces are used for padding. The fill function returns the prior padding character. The setfill manipulator also sets the padding character. Figure 15.16 demonstrates using member function fill (line 40) and stream manipulator setfill (lines 44 and 47) to set the fill character.

 1  // Fig. 15.16: Fig15_16.cpp
 2  // Using member function fill and stream manipulator setfill to change
 3  // the padding character for fields larger than the printed value.
 4  #include <iostream>
 5  using std::cout;
 6  using std::dec;
 7  using std::endl;
 8  using std::hex;
 9  using std::internal;
10  using std::left;
11  using std::right;
12  using std::showbase;
13
14  #include <iomanip>
15  using std::setfill;
16  using std::setw;
17
18  int main()
19  {
20     int x = 10000;
21
22     // display x
23     cout << x << " printed as int right and left justified\n"
24        << "and as hex with internal justification.\n"
25        << "Using the default pad character (space):" << endl;
26
27     // display x with base
28     cout << showbase << setw( 10 ) << x << endl;
29
30     // display x with left justification
31     cout << left << setw( 10 ) << x << endl;
32
33     // display x as hex with internal justification
34     cout << internal << setw( 10 ) << hex << x << endl << endl;
35
36     cout << "Using various padding characters:" << endl;
37
38     // display x using padded characters (right justification)
39     cout << right;
40     cout.fill( '*' );
41     cout << setw( 10 ) << dec << x << endl;
42
43     // display x using padded characters (left justification)
44     cout << left << setw( 10 ) << setfill( '%' ) << x << endl;
45
46     // display x using padded characters (internal justification)
47     cout << internal << setw( 10 ) << setfill( '^' ) << hex
48        << x << endl;
49     return 0;
50  } // end main
 Fig. 15.16  Using member function fill and stream manipulator setfill to change the padding character for fields larger than the values being printed.

 10000 printed as int right and left justified
 and as hex with internal justification.
 Using the default pad character (space):
      10000
 10000
 0x    2710

 Using various padding characters:
 *****10000
 10000%%%%%
 0x^^^^2710

Other sections in this tutorial:
Introduction to Stream Format States and Stream Manipulators
15.7.1 Trailing Zeros and Decimal Points (showpoint)
15.7.2 Justification (left, right and internal)
15.7.3 Padding (fill, setfill) (You are here).
15.7.4 Integral Stream Base (dec, oct, hex, showbase)
15.7.5 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed)
15.7.6 Uppercase/Lowercase Control (uppercase)
15.7.7 Specifying Boolean Format (boolalpha)
15.7.8 Setting and Resetting the Format State via Member Function flags

Tutorial Index