Sams Teach Yourself C++ in 21 Days

(singke) #1
This output is buffered until an end of line is read, however. When EOFis encountered
(by pressing Ctrl+Z on a DOS machine, or Ctrl+D on a Unix machine), the loop exits.
Note that not every implementation of istreamsupports this version of get(), although
it is now part of the ANSI/ISO standard.

Using get()with a Character Reference Parameter
When a character variable is passed as input to get(), that character variable is filled
with the next character in the input stream. The return value is an iostreamobject, and
so this form of get()can be concatenated, as illustrated in Listing 17.5.

LISTING17.5 Using get()with Parameters
0: // Listing 17.5 - Using get() with parameters
1:
2: #include <iostream>
3:
4: int main()
5: {
6: char a, b, c;
7:
8: std::cout << “Enter three letters: “;
9:
10: std::cin.get(a).get(b).get(c);
11:
12: std::cout << “a: “ << a << “\nb: “;
13: std::cout << b << “\nc: “ << c << std::endl;
14: return 0;
15: }

Enter three letters: one
a: o
b: n
c: e
On line 6, three character variables,a,b, and c, are created. On line 10,
cin.get()is called three times, concatenated. First,cin.get(a)is called. This
puts the first letter into aand returns cinso that when it is done,cin.get(b)is called,
putting the next letter into b. Finally,cin.get(c)is called and the third letter is put in c.
Because cin.get(a)evaluates to cin, you could have written this:
cin.get(a) >> b;
In this form,cin.get(a)evaluates to cin, so the second phrase is cin >> b;.

OUTPUT


606 Day 17


ANALYSIS
Free download pdf