|
|
|
|
|
|
|
(table continued from previous page) |
|
|
|
|
 |
|
|
|
|
cin >> x; |
|
|
|
| x = 16.9 | 25\n
A\n 16.9 | | 3. | | 5A16.9\n |  |
|
|
|
|
cin >> i; |
|
|
|
| i = 25 | 25 16.9\n |  |
|
|
|
|
cin >> ch; |
|
|
|
| ch = A | 25A 6.9\n |  |
|
|
|
|
cin >> x; |
|
|
|
| x = 16.9 | 25A16.9 |
|
|
|
|
|
|
Reading Character Data with the get Function |
|
|
|
|
|
|
|
|
As we have discussed, the >> operator skips any leading whitespace characters (such as blanks and newline characters) while looking for the next data value in the input stream. Suppose that ch1 and ch2 are char variables and the program executes the statement |
|
|
|
|
|
|
|
|
If the input stream consists of |
|
|
|
|
|
|
|
|
then the extraction operator stores R into chl, skips the blank, and stores 1 into ch2. (Note that the char value 1 is not the same as the int value 1. The two are stored completely differently in a computer's memory. The extraction operator interprets the same data in different ways, depending on the data type of the variable that's being filled.) |
|
|
|
|
|
|
|
|
What if we had wanted to input three characters from the input line: the R, the blank, and the 1? With the extraction operator, it's not possible. Whitespace characters such as blanks are skipped over. |
|
|
|
|
|
|
|
|
The istream data type provides a second way in which to read character data, in addition to the >> operator. You can use the get function, which inputs the very next character in the input stream without skipping any whitespace characters. A function call looks like this: |
|
|
|
|
|
|
|
|
You give the name of an istream variable (here, cin), then a dot (period), and then the function name and parameter list. Notice that the call to get uses |
|
|
|
|
|