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
Manipulator
Action Performed
dec
Formats integer values as base 10 (decimal). This is the default
representation.
hex
Formats integer values as base 16 (hexadecimal).
oct
Formats integer values as base 8 (octal).
left
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
Right-aligns values in the output field and pads them on the left with the
fill character. This is the default alignment.
fixed
Outputs floating-point values in fixed-point notation—that is, without an
exponent.
scientific
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.
showpoint
Shows the decimal point and trailing zeros for floating-point values.
noshowpoint
The opposite of the showpoint manipulator. This is the default.
showbase
Prefixes octal output with 0 and hexadecimal output with 0x or 0X.
noshowbase
Shows octal and hexadecimal output without the prefix. This is the
default.
showpos
Shows plus signs (+) for positive values.
noshowpos
Doesn’t show plus signs for positive values. This is the default.
uppercase
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.
nouppercase
Uses lowercase for the preceding items. This is the default.
boolalpha
Displays bool values as true and false.
noboolalpha
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
Manipulator
Action Performed
setfill()
Sets the fill character as specified by the argument. The default fill
character is a space.
setw()
Sets the field width as specified by the argument.
setprecision()
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.