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.2 Justification (left, right and internal)

Stream manipulators left and right enable fields to be left justified with padding characters to the right or right justified with padding characters to the left, respectively. The padding character is specified by the fill member function or the setfill parameterized stream manipulator (which we discuss in Section 15.7.3). Figure 15.14 uses the setw, left and right manipulators to left justify and right justify integer data in a field.

 1  // Fig. 15.14: Fig15_14.cpp
 2  // Demonstrating left justification and right justification.
 3  #include <iostream>
 4  using std::cout;
 5  using std::endl;
 6  using std::left;
 7  using std::right;
 8
 9  #include <iomanip>
10  using std::setw;
11
12  int main()
13  {
14     int x = 12345;
15
16     // display x right justified (default)
17     cout << "Default is right justified:" << endl
18        << setw( 10 ) << x;
19
20     // use left manipulator to display x left justified
21     cout << "\n\nUse std::left to left justify x:\n"
22        << left << setw( 10 ) << x;
23
24     // use right manipulator to display x right justified
25     cout << "\n\nUse std::right to right justify x:\n"
26        << right << setw( 10 ) << x << endl;
27     return 0;
28  } // end main
 Fig. 15.14  Left justification and right justification with stream manipulators left and right.

 Default is right justified:
      12345

 Use std::left to left justify x:
 12345

 Use std::right to right justify x:
      12345

Stream manipulator internal indicates that a number’s sign (or base when using stream manipulator showbase) should be left justified within a field, that the number’s magnitude should be right justified and that intervening spaces should be padded with the fill character. Figure 15.15 shows the internal stream manipulator specifying internal spacing (line 15). Note that showpos forces the plus sign to print (line 15). To reset the showpos setting, output the stream manipulator noshowpos.

 1  // Fig. 15.15: Fig15_15.cpp
 2  // Printing an integer with internal spacing and plus sign.
 3  #include <iostream>
 4  using std::cout;
 5  using std::endl;
 6  using std::internal;
 7  using std::showpos;
 8
 9  #include <iomanip>
10  using std::setw;
11
12  int main()
13  {
14     // display value with internal spacing and plus sign
15     cout << internal << showpos << setw( 10 ) << 123 << endl;
16     return 0;
17  } // end main
 Fig. 15.15  Printing an integer with internal spacing and plus sign.

 +      123
 

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) (You are here).
15.7.3 Padding (fill, setfill)
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