|
|
|
|
|
|
|
Here are some examples that use a char variable ch and three int variables, i, j, and k: |
|
|
|
|
| Statements | Contents After
Input | Marker Position in
the Input Stream | | 1. | | 957 34 1235\n 128 96\n |  |
|
|
|
|
cin >> i >> j; |
|
|
|
| i = 957, j = 34 | 957 1235\n 128 96\n |  |
|
|
|
|
cin.ignore(100, \n); |
|
|
|
| | 957 34 1235\n
28 96\n |  |
|
|
|
|
cin >> k; |
|
|
|
| k = 128 | 957 34 1235\n 128 96\n | | 2. | | 22 B 16 C 19\n |  |
|
|
|
|
cin >> ch; |
|
|
|
| ch = A | A 22 B 16 C 19\n |  |
|
|
|
|
cin.ignore(100, B); |
|
|
|
| | A 22 B 16 C 19\n |  |
|
|
|
|
cin >> i; |
|
|
|
| i = 16 | A 22 B 16 C 19\n | | 3. | | BCDEF\n |  |
|
|
|
|
cin.ignore(2, \n); |
|
|
|
| | AB DEF\n |  |
|
|
|
|
cin >> ch; |
|
|
|
| ch = C | ABC EF\n |
|
|
|
|
|
|
Example (1) shows the most common use of the ignore function, which is to skip the rest of the data on the current input line. Example (2) demonstrates the use of a character other than \n as the second parameter. We skip over all input characters until a B has been found, then read the next input number into i. In both (1) and (2), we are focusing on the second parameter to the ignore function, and we arbitrarily choose any large number like 100 for the first parameter. In (3), we change our focus and concentrate on the first parameter. Our intention is to skip the next two input characters on the current line. |
|
|
|
|
|
|
|
|
Remember in Chapter 1 that we defined an interactive program as one in which the user communicates directly with the computer. Many of the programs that you write will be interactive. There is a certain etiquette in- |
|
|
|
|
|