1
The member function tt(setf()) is used to define the way numbers are
2
displayed. It expects one or two arguments, all em(flags) of the tt(iostream)
3
class. In the following examples, tt(cout) is used, but other tt(ostream)
4
objects might have been used as well:
7
it() To display the numeric base of integral values, use
8
centt(cout.setf(ios::showbase))
9
This results in em(no) prefix for decimal values, tt(0x) for hexadecimal
10
values, tt(0) for octal values. For example:
12
cout.setf(ios::showbase);
13
cout << 16 << ", " << hex << 16 << ", " << oct << 16 << '\n';
17
it() To display a trailing decimal point and trailing decimal zeros when
18
real numbers are displayed, use
19
centt(cout.setf(ios::showpoint))
22
cout.setf(ios::showpoint);
23
cout << 16.0 << ", " << 16.1 << ", " << 16 << '\n';
26
centt(16.0000, 16.1000, 16)
27
Note that the last tt(16) is an integral rather than a real number, and is not
28
given a decimal point.
30
If tt(ios::showpoint) is not used, then trailing zeros are discarded. If the
31
decimal part is zero, then the decimal point is discarded as well.
32
it() Comparable to the tt(dec, hex) and tt(oct) manipulators
34
cout.setf(ios::dec, ios::basefield);
35
cout.setf(ios::hex, ios::basefield);
39
cout.setf(ios::oct, ios::basefield);
42
it() To control the way real numbers are displayed tt(cout.setf(ios::fixed,
43
ios::floatfield)) or tt(cout.setf(ios::scientific, ios::floatfield)) can be
44
used. These settings result in, respectively, a fixed value display or a
45
scientific (power of 10) display of numbers. For example,
47
cout.setf(ios::fixed, ios::floatfield);
48
cout << sqrt(200) << '\n';
49
cout.setf(ios::scientific, ios::floatfield);
50
cout << sqrt(200) << '\n';
57
itt(ios::left): This format state is used to left-adjust the display
58
of values for which the tt(setw()) manipulator (see below) is used. The format
59
state can be set using the tt(setf()) member function, and it can be unset
60
using the tt(unsetf()) member function. By default values are right-adjusted.
62
itt(ios::internal): This format state will add the fill-characters
63
(blanks by default) between the minus sign of negative numbers and the value
68
With tt(istream) objects the flag tt(ios::skipws) can be used to control the
69
handling of whitespace characters when characters are extracted. Leading white
70
space characters of numerical values are skipped when
71
tt(istreamObject.unsetf(ios::skipws)) has been specified, but otherwise they
72
must be read explicitly. Reading a tt(char *) or tt(string) variable in this
73
situation will only succeed if the first character to be read isn't a
74
white-space character. The following small program can be used to illustrate
75
the effects of unsetting tt(ios::skipws):
89
cin.unsetf(ios::skipws);
91
cin >> i; // skips leading ws
92
cin >> buffer; // doesn't skip leading ws.
94
cout << "got " << i << " and " << buffer << '\n';
96
while (cin >> c) // reads individual chars, if the previous
97
cout << "got '" << c << "'\n"; // extraction succeeded.
106
itt(setf(ios::showbase)) is used to display the numeric base of integral
108
itt(setf(ios::showpoint)) is used to display the trailing decimal point
109
and trailing zeros of real numbers
110
itt(setf(ios::dec, ios::basefield), setf(ios::hex, ios::basefield)) and
111
tt(setf(ios::oct, ios::basefield)) can be used instead of the tt(dec, hex) and
112
tt(oct) manipulators.
113
itt(cout.setf(ios::scientific, ios::floatfield)) and
114
tt(cout.setf(ios::fixed, ios::floatfield)) can be used to obtain a fixed or
115
scientific (power of 10) display of real values.
116
itt(setf(ios::left)) is used to left-adjust values in the width of
118
itt(setf(ios::internal)) is used to left-adjust the minus sign of negative
119
values (while the values themselves are right adjusted).
120
itt(ios::skipws) is used to control the handling of white space characters
121
by the extraction operator.
124
To em(unset) flags, the function tt(unsetf()) can be used.