Working with Streams 605
17
A common use of get()with no parameters is illustrated in Listing 17.4.
LISTING17.4 Using get()with No Parameters
0: // Listing 17.4 - Using get() with no parameters
1:
2: #include <iostream>
3:
4: int main()
5: {
6: char ch;
7: while ( (ch = std::cin.get()) != EOF)
8: {
9: std::cout << “ch: “ << ch << std::endl;
10: }
11: std::cout << “\nDone!\n”;
12: return 0;
13: }
To exit this program, you must send end of file from the keyboard. On DOS
computers, use Ctrl+Z; on Unix workstations, use Ctrl+D.
TIP
Hello
ch: H
ch: e
ch: l
ch: l
ch: o
ch:
World
ch: W
ch: o
ch: r
ch: l
ch: d
ch:
^Z (ctrl-z)
Done!
On line 6, a local character variable,ch, is declared. The whileloop assigns the
input received from cin.get()to ch, and if it is not EOF, the string is printed out.
OUTPUT
ANALYSIS