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.4 Integral Stream Base (dec, oct, hex, showbase)

C++ provides stream manipulators dec, hex and oct to specify that integers are to be displayed as decimal, hexadecimal and octal values, respectively. Stream insertions default to decimal if none of these manipulators is used. With stream extraction, integers prefixed with 0 (zero) are treated as octal values, integers prefixed with 0x or 0X are treated as hexadecimal values, and all other integers are treated as decimal values. Once a particular base is specified for a stream, all integers on that stream are processed using that base until a different base is specified or until the program terminates.

Stream manipulator showbase forces the base of an integral value to be output. Decimal numbers are output by default, octal numbers are output with a leading 0, and hexadecimal numbers are output with either a leading 0x or a leading 0X (as we discuss in Section 15.7.6, stream manipulator uppercase determines which option is chosen). Figure 15.17 demonstrates the use of stream manipulator showbase to force an integer to print in decimal, octal and hexadecimal formats. To reset the showbase setting, output the stream manipulator noshowbase.

 1  // Fig. 15.17: Fig15_17.cpp
 2  // Using stream manipulator showbase.
 3  #include <iostream>
 4  using std::cout;
 5  using std::endl;
 6  using std::hex;
 7  using std::oct;
 8  using std::showbase;
 9
10  int main()
11  {
12     int x = 100;
13
14     // use showbase to show number base
15     cout << "Printing integers preceded by their base:" << endl
16        << showbase;
17
18     cout << x << endl; // print decimal value
19     cout << oct << x << endl; // print octal value
20     cout << hex << x << endl; // print hexadecimal value
21     return 0;
22  } // end main
 Fig. 15.17  Stream manipulator showbase.

 Printing integers preceded by their base:
 100
 0144
 0x64

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)
15.7.4 Integral Stream Base (dec, oct, hex, showbase) (You are here).
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