?? ch16.htm
字號:
extraction operation can be the input to the next extraction.</P>
<PRE><FONT COLOR="#0066FF">int VarOne, varTwo, varThree;
cout << "Enter three numbers: "
cin >> VarOne >> varTwo >> varThree;
</FONT></PRE>
<P>When you write <TT>cin >> VarOne >> varTwo >> varThree;</TT>,
the first extraction is evaluated <TT>(cin >> VarOne)</TT>. The return value
from this is another <TT>istream</TT> object, and that object's extraction operator
gets the variable <TT>varTwo</TT>. It is as if you had written this:</P>
<PRE><FONT COLOR="#0066FF">((cin >> varOne) >> varTwo) >> varThree;
</FONT></PRE>
<P>You'll see this technique repeated later when <TT>cout</TT> is discussed.
<H3 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">Other Member Functions
of cin</FONT></H3>
<P>In addition to overloading <TT>operator>></TT>, <TT>cin</TT> has a number
of other member functions. These are used when finer control over the input is required.
<H4 ALIGN="CENTER"><A NAME="Heading25"></A><FONT COLOR="#000077">Single Character
Input</FONT></H4>
<P><TT>operator>></TT> taking a character reference can be used to get a single
character from the standard input. The member function <TT>get()</TT> can also be
used to obtain a single character, and can do so in two ways. <TT>get()</TT> can
be used with no parameters, in which case the return value is used, or it can be
used with a reference to a character. Using get() with No Parameters The first form
of <TT>get()</TT> is without parameters. This returns the value of the character
found, and will return <TT>EOF</TT> (end of file) if the end of the file is reached.
<TT>get()</TT> with no parameters is not often used. It is not possible to concatenate
this use of <TT>get()</TT> for multiple input, because the return value is not an
<TT>iostream</TT> object. Thus, the following won't work:</P>
<PRE><FONT COLOR="#0066FF">cin.get() >>myVarOne >> myVarTwo; // illegal
</FONT></PRE>
<P>The return value of (<TT>cin.get() >> myVarOne</TT>) is an integer, not
an <TT>iostream</TT> object.</P>
<P>A common use of <TT>get()</TT> with no parameters is illustrated in Listing 16.4.</P>
<P><A NAME="Heading26"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 16.4. Using
get() with no parameters.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1: // Listing 16.4 - Using get() with no parameters
2: #include <iostream.h>
3:
4: int main()
5: {
6: char ch;
7: while ( (ch = cin.get()) != EOF)
8: {
9: cout << "ch: " << ch << endl;
10: }
11: cout << "\nDone!\n";
12: return 0;
<TT>13: }</TT></FONT></PRE>
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>To exit this program, you must send
end of file from the keyboard. On DOS computers use Ctrl+Z; on UNIX units use Ctrl+D.
<HR>
</BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF">Output: Hello
ch: H
ch: e
ch: l
ch: l
ch: o
ch:
World
ch: W
ch: o
ch: r
ch: l
ch: d
ch:
(ctrl-z)
Done!
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis</B></FONT><FONT COLOR="#000000"><B>:</B></FONT><FONT
COLOR="#000077"><B> </B></FONT>On line 6, a local character variable is declared.
The <TT>while</TT> loop assigns the input received from <TT>cin.get()</TT> to <TT>ch</TT>,
and if it is not <TT>EOF</TT> the string is printed out. This output is buffered
until an end of line is read, however. Once <TT>EOF</TT> is encountered (by pressing
Ctrl+Z on a DOS machine, or Ctrl+D on a UNIX machine), the loop exits.</P>
<P>Note that not every implementation of <TT>istream</TT> supports this version of
<TT>get()</TT>. Using get() with a Character Reference Parameter When a character
is passed as input to <TT>get()</TT>, that character is filled with the next character
in the input stream. The return value is an <TT>iostream</TT> object, and so this
form of <TT>get()</TT> can be concatenated, as illustrated in Listing 16.5.</P>
<P><A NAME="Heading27"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 16.5 Using get()
with parameters.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1: // Listing 16.5 - Using get() with parameters
2: #include <iostream.h>
3:
4: int main()
5: {
6: char a, b, c;
7:
8: cout << "Enter three letters: ";
9:
10: cin.get(a).get(b).get(c);
11:
12: cout << "a: " << a << "\nb: " << b << "\nc: " << c << endl;
13: return 0;
<TT>14: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: Enter three letters: one
a: o
b: n
c: e
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis</B></FONT><FONT COLOR="#000000"><B>:</B></FONT><B>
</B>On line 6, three character variables are created. On line 10, <TT>cin.get()</TT>
is called three times, concatenated. First <TT>cin.get(a)</TT> is called. This puts
the first letter into <TT>a</TT> and returns <TT>cin</TT> so that when it is done,
<TT>cin.get(b)</TT> is called, putting the next letter into <TT>b</TT>. The end result
of this is that <TT>cin.get(c)</TT> is called and the third letter is put in <TT>c</TT>.</P>
<P>Because <TT>cin.get(a)</TT> evaluates to <TT>cin</TT>, you could have written
this:</P>
<PRE><FONT COLOR="#0066FF">cin.get(a) >> b;</FONT></PRE>
<P>In this form, <TT>cin.get(a)</TT> evaluates to <TT>cin</TT>, so the second phrase
is <TT>cin >> b;</TT>.
<BLOCKQUOTE>
<P>
<HR>
<B>DO </B>use the extraction operator (<TT>>></TT>) when you need to skip over
white space. <B>DO</B> use <TT>get()</TT> with a character parameter when you need
to examine every character, including white space. <B>DON'T </B>use <TT>get()</TT>
with no parameters at all; it is more or less obsolete.
<HR>
</BLOCKQUOTE>
<H4 ALIGN="CENTER"><A NAME="Heading28"></A><FONT COLOR="#000077">Getting Strings
from Standard Input</FONT></H4>
<P>The extraction operator (<TT>>></TT>) can be used to fill a character array,
as can the member functions <TT>get()</TT> and <TT>getline()</TT>.</P>
<P>The final form of <TT>get()</TT> takes three parameters. The first parameter is
a pointer to a character array, the second parameter is the maximum number of characters
to read plus one, and the third parameter is the termination character.</P>
<P>If you enter <TT>20</TT> as the second parameter, <TT>get()</TT> will read 19
characters and then will null-terminate the string, which it will store in the first
parameter. The third parameter, the termination character, defaults to newline (<TT>`\n'</TT>).
If a termination character is reached before the maximum number of characters is
read, a null is written and the termination character is left in the buffer.</P>
<P>Listing 16.6 illustrates the use of this form of <TT>get()</TT>.</P>
<P><A NAME="Heading29"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 16.6. Using
get() with a character array.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1: // Listing 16.6 - Using get() with a character array
2: #include <iostream.h>
3:
4: int main()
5: {
6: char stringOne[256];
7: char stringTwo[256];
8:
9: cout << "Enter string one: ";
10: cin.get(stringOne,256);
11: cout << "stringOne: " << stringOne << endl;
12:
13: cout << "Enter string two: ";
14: cin >> stringTwo;
15: cout << "StringTwo: " << stringTwo << endl;
16: return 0;
<TT>17: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: Enter string one: Now is the time
stringOne: Now is the time
Enter string two: For all good
StringTwo: For
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis</B></FONT><FONT COLOR="#000000"><B>:</B></FONT><B>
</B>On lines 6 and 7, two character arrays are created. On line 9, the user is prompted
to enter a string, and <TT>cin.get()</TT> is called on line 10. The first parameter
is the buffer to fill, and the second is one more than the maximum number for <TT>get()</TT>
to accept (the extra position being given to the null character, (<TT>`\0'</TT>)).
The defaulted third parameter is a newline.</P>
<P>The user enters <TT>Now is the time</TT>. Because the user ends the phrase with
a newline, that phrase is put into <TT>stringOne</TT>, followed by a terminating
null.</P>
<P>The user is prompted for another string on line 13, and this time the extraction
operator is used. Because the extraction operator takes everything up to the first
white space, the string <TT>For</TT>, with a terminating null character, is stored
in the second string, which of course is not what was intended.</P>
<P>Another way to solve this problem is to use <TT>getline()</TT>, as illustrated
in Listing 16.7.</P>
<P><A NAME="Heading31"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 16.7. Using
getline().</B></FONT></P>
<PRE><FONT COLOR="#0066FF">1: // Listing 16.7 - Using getline()
2: #include <iostream.h>
3:
4: int main()
5: {
6: char stringOne[256];
7: char stringTwo[256];
8: char stringThree[256];
9:
10: cout << "Enter string one: ";
11: cin.getline(stringOne,256);
12: cout << "stringOne: " << stringOne << endl;
13:
14: cout << "Enter string two: ";
15: cin >> stringTwo;
16: cout << "stringTwo: " << stringTwo << endl;
17:
18: cout << "Enter string three: ";
19: cin.getline(stringThree,256);
20: cout << "stringThree: " << stringThree << endl;
21: return 0;
<TT>22: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: Enter string one: one two three
stringOne: one two three
Enter string two: four five six
stringTwo: four
Enter string three: stringThree: five six
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis</B></FONT><FONT COLOR="#000000"><B>:</B></FONT><B>
</B>This example warrants careful examination; there are some potential surprises.
On lines 6-8, three character arrays are declared.</P>
<P>On line 10, the user is prompted to enter a string, and that string is read by
<TT>getline()</TT>. Like <TT>get()</TT>, <TT>getline()</TT> takes a buffer and a
maximum number of characters. Unlike <TT>get()</TT>, however, the terminating newline
is read and thrown away. With <TT>get()</TT> the terminating newline is not thrown
away. It is left in the input buffer.</P>
<P>On line 14, the user is prompted again, and this time the extraction operator
is used. The user enters <TT>four five six</TT>, and the first word, <TT>four</TT>,
is put in <TT>stringTwo</TT>. The string <TT>Enter string three</TT> is then displayed,
and <TT>getline()</TT> is called again. Because <TT>five six</TT> is still in the
input buffer, it is immediately read up to the newline; <TT>getline()</TT> terminates
and the string in <TT>stringThree</TT> is printed on line 20.</P>
<P>The user has no chance to enter string three, because the second <TT>getline()</TT>
call is fulfilled by the string remaining in the input buffer after the call to the
extraction operator on line 15.</P>
<P>The extraction operator (<TT>>></TT>) reads up to the first white space
and puts the word into the character array.</P>
<P>The member function <TT>get()</TT> is overloaded. In one version, it takes no
parameters and returns the value of the character it receives. In the second version,
it takes a single character reference and returns the <TT>istream</TT> object by
reference.</P>
<P>In the third and final version, <TT>get()</TT> takes a character array, a number
of characters to get, and a termination character (which defaults to newline). This
version of <TT>get()</TT> reads characters into the array until it gets to one fewer
than its maximum number of characters or it encounters the termination character,
whichever comes first. If <TT>get()</TT> encounters the termination character, it
leaves that character in the input buffer and stops reading characters.</P>
<P>The member function <TT>getline()</TT> also takes three parameters: the buffer
to fill, one more than the maximum number of characters to get, and the termination
character. <TT>getline()</TT>functions exactly like <TT>get()</TT> does with these
parameters, except <TT>getline()</TT> throws away the terminating character.
<H4 ALIGN="CENTER"><A NAME="Heading32"></A><FONT COLOR="#000077">Using cin.ignore()</FONT></H4>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -