Deitel & Associates, Inc. Logo

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

© 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.7 Specifying Boolean Format (boolalpha)

C++ provides data type bool, whose values may be false or true, as a preferred alternative to the old style of using 0 to indicate false and nonzero to indicate true. A bool variable outputs as 0 or 1 by default. However, we can use stream manipulator boolalpha to set the output stream to display bool values as the strings “true” and “false”. Use stream manipulator noboolalpha to set the output stream to display bool values as integers (i.e., the default setting). The program of Fig. 15.20 demonstrates these stream manipulators. Line 14 displays the bool value, which line 11 sets to true, as an integer. Line 18 uses manipulator boolalpha to display the bool value as a string. Lines 21–22 then change the bool’s value and use manipulator noboolalpha, so line 25 can display the bool value as an integer. Line 29 uses manipulator boolalpha to display the bool value as a string. Both boolalpha and noboolalpha are “sticky” settings.

Good Programming Practice
Good Programming Practice 15.1
Displaying bool values as true or false, rather than nonzero or 0, respectively, makes program outputs clearer.
 1  // Fig. 15.20: Fig15_20.cpp
 2  // Demonstrating stream manipulators boolalpha and noboolalpha.
 3  #include <iostream>
 4  using std::boolalpha;
 5  using std::cout;
 6  using std::endl;
 7  using std::noboolalpha;
 9  int main()
10  {
11     bool booleanValue = true;
13     // display default true booleanValue
14     cout << "booleanValue is " << booleanValue << endl;
16     // display booleanValue after using boolalpha
17     cout << "booleanValue (after using boolalpha) is "
18        << boolalpha << booleanValue << endl << endl;
20     cout << "switch booleanValue and use noboolalpha" << endl;
21     booleanValue = false; // change booleanValue
22     cout << noboolalpha << endl; // use noboolalpha
24     // display default false booleanValue after using noboolalpha
25     cout << "booleanValue is " << booleanValue << endl;
27     // display booleanValue after using boolalpha again
28     cout << "booleanValue (after using boolalpha) is "
29        << boolalpha << booleanValue << endl;
30     return 0;
31  } // end main
 Fig. 15.20  Stream manipulators boolalpha and noboolalpha.

 booleanValue is 1
 booleanValue (after using boolalpha) is true

 switch booleanValue and use noboolalpha

 booleanValue is 0
 booleanValue (after using boolalpha) is false

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)
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) (You are here).
15.7.8 Setting and Resetting the Format State via Member Function flags

Tutorial Index