~ubuntu-branches/ubuntu/trusty/c++-annotations/trusty-proposed

« back to all changes in this revision

Viewing changes to yo/iostreams/src/setf.yo.OFF

  • Committer: Package Import Robot
  • Author(s): Frank B. Brokken
  • Date: 2013-05-30 13:32:18 UTC
  • mfrom: (1.1.24)
  • Revision ID: package-import@ubuntu.com-20130530133218-k39mr5uredd093jr
Tags: 9.7.2-1
New upstream release, repairs several minor left-over flaws.
This release incorporates 9.7.0 and 9.7.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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:
5
 
 
6
 
startit()
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:
11
 
verb(
12
 
    cout.setf(ios::showbase);
13
 
    cout << 16 << ", " << hex << 16 << ", " << oct << 16 << '\n';
14
 
)
15
 
results in:
16
 
        centt(16, 0x10, 020)
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))
20
 
For example:
21
 
verb(
22
 
    cout.setf(ios::showpoint);
23
 
    cout << 16.0 << ", " << 16.1 << ", " << 16 << '\n';
24
 
)
25
 
results in:
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.
29
 
 
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
33
 
verb(
34
 
    cout.setf(ios::dec, ios::basefield);
35
 
    cout.setf(ios::hex, ios::basefield);
36
 
)
37
 
or
38
 
verb(
39
 
    cout.setf(ios::oct, ios::basefield);
40
 
)
41
 
can be used.
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,
46
 
verb(
47
 
    cout.setf(ios::fixed, ios::floatfield);
48
 
    cout << sqrt(200) << '\n';
49
 
    cout.setf(ios::scientific, ios::floatfield);
50
 
    cout << sqrt(200) << '\n';
51
 
)
52
 
results in
53
 
verb(
54
 
    14.142136
55
 
    1.414214e+01
56
 
)
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.
61
 
 
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
64
 
itself.
65
 
 
66
 
endit()
67
 
 
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):
76
 
    verb(
77
 
#include <iostream>
78
 
#include <string>
79
 
 
80
 
int main()
81
 
{
82
 
    string
83
 
        buffer;
84
 
    int
85
 
        i;
86
 
    char
87
 
        c;
88
 
 
89
 
    cin.unsetf(ios::skipws);
90
 
 
91
 
    cin >> i;           // skips leading ws
92
 
    cin >> buffer;      // doesn't skip leading ws.
93
 
 
94
 
    cout << "got " << i << " and " << buffer << '\n';
95
 
 
96
 
    while (cin >> c)    // reads individual chars, if the previous
97
 
        cout << "got '" << c << "'\n";    // extraction succeeded.
98
 
 
99
 
    return (0);
100
 
}
101
 
    )
102
 
 
103
 
 
104
 
Summarizing:
105
 
    startit()
106
 
    itt(setf(ios::showbase)) is used to display the numeric base of integral
107
 
values,
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
117
 
their fields
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.
122
 
    endit()
123
 
 
124
 
To em(unset) flags, the function tt(unsetf()) can be used.