Have you ever wanted to learn how basic types of C++ variables interact in complex situations? Ivor Horton explains this, and also describes some interesting features of C++. This article is from chapter 3 of Ivor Horton's Beginning ANSC C++ The Complete Language (Apress, 2004; ISBN 1590592271).
More on Handling Basic Data Types - More on Output Manipulators (Page 8 of 13 )
Taking the last chapter into account, you’ve now seen five of the modal output manipulators that the <iostream> header defines: scientific, fixed, dec, hex, and oct. The time seems right, therefore, to list these and all the other similar manipulators in one place (see Table 3-2). Don’t worry for now about the bool values mentioned in the last two entries—they’re coming up in the next chapter.
Table 3-2. Output Manipulators
Formats integer values as base 10 (decimal). This is the default
Formats integer values as base 16 (hexadecimal).
Formats integer values as base 8 (octal).
Left-aligns values in the output field and pads them on the right with the
fill character. The default fill character is a space, as you’ve seen.
Right-aligns values in the output field and pads them on the left with the
fill character. This is the default alignment.
Outputs floating-point values in fixed-point notation—that is, without an
Outputs floating-point values in scientific notation—that is, as the
mantissa plus an exponent. The default mode selects fixed or scientific
notation, depending on the value to be displayed.
Shows the decimal point and trailing zeros for floating-point values.
The opposite of the showpoint manipulator. This is the default.
Prefixes octal output with 0 and hexadecimal output with 0x or 0X.
Shows octal and hexadecimal output without the prefix. This is the
Shows plus signs (+) for positive values.
Doesn’t show plus signs for positive values. This is the default.
Displays uppercase A through F for hexadecimal digits when outputting
integers in hexadecimal format and 0X if showbase is set. Displays E for
the exponent when outputting values in scientific notation, rather than
using lowercase e.
Uses lowercase for the preceding items. This is the default.
Displays bool values as true and false.
Displays bool values as 1 and 0.
You may want to set more than one of these modes at a time, and one way to do this is to insert multiple manipulators into the stream. For example, if you want to output your integer data as hexadecimal values that are left aligned in the output field, you could write
std::cout << std::hex << std::left << value;
which will output value (and all subsequent integers in the program, unless the settings are changed) as a left-justified hexadecimal number.
Table 3-3 shows the manipulators that expect you to supply an argument value.
Table 3-3. Output Manipulators That Require an Argument Value
Sets the fill character as specified by the argument. The default fill
character is a space.
Sets the field width as specified by the argument.
Sets the precision for floating-point values as specified by the argument.
The precision is the number of decimal digits in the output.
This article is excerpted from Beginning ANSI C++ The Complete Language by Ivor Horton (Apress, 2004; ISBN 1590592271). Check it out at your favorite bookstore today. Buy this book now.