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

« back to all changes in this revision

Viewing changes to yo/iostreams/src/extraction.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
 
With the em(extraction) operator, a similar situation holds true as with the
2
 
insertion operator, the extraction operator operating comparably to the
3
 
tt(scanf()) function. I.e., white space characters are skipped. Also, the
4
 
operator doesn't expect em(pointers) to variables that should be given new
5
 
values, but em(references) (with the exception of the tt(char *), but
6
 
tt(string) variables are used as references).
7
 
 
8
 
Consider the following code:
9
 
 
10
 
verb(
11
 
    int
12
 
        i1,
13
 
        i2;
14
 
    char
15
 
        c;
16
 
 
17
 
    cin >> i1 >> i2;                // see (1)
18
 
 
19
 
    while (cin >> c && c != '.')    // see (2)
20
 
        process(c);
21
 
 
22
 
    char                            // see (3)
23
 
        buffer[80];
24
 
                                    // see (3)
25
 
    while (cin >> buffer)
26
 
        process(buffer);
27
 
)
28
 
 
29
 
This example shows several characteristics of the extraction operator worth
30
 
noting. Assume the input consists of the following lines:
31
 
 
32
 
verb(
33
 
    125
34
 
    22
35
 
    h e l l o
36
 
    w o r l d .
37
 
    this example shows
38
 
    that we're not yet done
39
 
    with C++
40
 
)
41
 
 
42
 
starteit()
43
 
    eit() In the first part of the example two int values are extracted
44
 
        from the input:
45
 
        these values are assigned, respectively, to tt(i1)  and tt(i2).
46
 
        White-space (newlines, spaces, tabs) is skipped, and the values
47
 
        125 and 22 are assigned to tt(i1) and tt(i2).
48
 
 
49
 
        If the assignment em(fails), e.g., when there are no numbers to be
50
 
        converted, the result of the extraction operator evaluates to a zero
51
 
        result, which can be used for testing purposes, as in:
52
 
 
53
 
        center(tt(if (!(cin >> i1))))
54
 
    eit() In the second part, characters are read. However, white space is
55
 
        skipped, so the characters of the words tt(hello) and tt(world) are
56
 
        produced by tt(cin), but the blanks that appear in between are not.
57
 
 
58
 
        Furthermore, the final tt('.') is not processed, since that one's
59
 
        used as a sentinel: the delimiter to end the tt(while)-loop, when the
60
 
        extraction is still successful.
61
 
    eit() In the third part, the argument of the extraction operator is yet
62
 
        another type of variable: when a tt(char *) is passed, white-space
63
 
        delimited strings are extracted. So, here the words tt(this, example,
64
 
        shows, that, we're, not, yet, done, with) and tt(C++) are returned.
65
 
 
66
 
        Then, the end of the information is reached. This has two consequences:
67
 
        First, the tt(while)-loop terminates. Second, an empty string is
68
 
        copied into the tt(buffer) variable.
69
 
endeit()