📄 ch16.htm
字号:
<PRE><FONT COLOR="#0066FF">cin.get() >>myVarOne >> myVarTwo; // illegal</FONT></PRE><P>The return value of (<TT>cin.get() >> myVarOne</TT>) is an integer, notan <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. Usingget() with no parameters.</B></FONT></P><PRE><FONT COLOR="#0066FF">1: // Listing 16.4 - Using get() with no parameters2: #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: Helloch: Hch: ech: lch: lch: och:Worldch: Wch: och: rch: lch: dch: (ctrl-z)Done!</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis</B></FONT><FONT COLOR="#000000"><B>:</B></FONT><FONTCOLOR="#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 buffereduntil an end of line is read, however. Once <TT>EOF</TT> is encountered (by pressingCtrl+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 characteris passed as input to <TT>get()</TT>, that character is filled with the next characterin the input stream. The return value is an <TT>iostream</TT> object, and so thisform 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 parameters2: #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: onea: ob: nc: 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 putsthe 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 resultof 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 writtenthis:</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 phraseis <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 Stringsfrom 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 isa pointer to a character array, the second parameter is the maximum number of charactersto 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 19characters and then will null-terminate the string, which it will store in the firstparameter. The third parameter, the termination character, defaults to newline (<TT>`\n'</TT>).If a termination character is reached before the maximum number of characters isread, 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. Usingget() with a character array.</B></FONT></P><PRE><FONT COLOR="#0066FF">1: // Listing 16.6 - Using get() with a character array2: #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 timestringOne: Now is the timeEnter string two: For all goodStringTwo: 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 promptedto enter a string, and <TT>cin.get()</TT> is called on line 10. The first parameteris 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 witha newline, that phrase is put into <TT>stringOne</TT>, followed by a terminatingnull.</P><P>The user is prompted for another string on line 13, and this time the extractionoperator is used. Because the extraction operator takes everything up to the firstwhite space, the string <TT>For</TT>, with a terminating null character, is storedin 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 illustratedin Listing 16.7.</P><P><A NAME="Heading31"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 16.7. Usinggetline().</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 threestringOne: one two threeEnter string two: four five sixstringTwo: fourEnter 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 amaximum number of characters. Unlike <TT>get()</TT>, however, the terminating newlineis read and thrown away. With <TT>get()</TT> the terminating newline is not thrownaway. It is left in the input buffer.</P><P>On line 14, the user is prompted again, and this time the extraction operatoris 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 theinput buffer, it is immediately read up to the newline; <TT>getline()</TT> terminatesand 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 theextraction operator on line 15.</P><P>The extraction operator (<TT>>></TT>) reads up to the first white spaceand puts the word into the character array.</P><P>The member function <TT>get()</TT> is overloaded. In one version, it takes noparameters 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 byreference.</P><P>In the third and final version, <TT>get()</TT> takes a character array, a numberof characters to get, and a termination character (which defaults to newline). Thisversion of <TT>get()</TT> reads characters into the array until it gets to one fewerthan its maximum number of characters or it encounters the termination character,whichever comes first. If <TT>get()</TT> encounters the termination character, itleaves that character in the input buffer and stops reading characters.</P><P>The member function <TT>getline()</TT> also takes three parameters: the bufferto fill, one more than the maximum number of characters to get, and the terminationcharacter. <TT>getline()</TT>functions exactly like <TT>get()</TT> does with theseparameters, except <TT>getline()</TT> throws away the terminating character.<H4 ALIGN="CENTER"><A NAME="Heading32"></A><FONT COLOR="#000077">Using cin.ignore()</FONT></H4><P>At times you want to ignore the remaining characters on a line until you hit eitherend of line (EOL) or end of file (EOF). The member function <TT>ignore()</TT> servesthis purpose. <TT>ignore()</TT> takes two parameters, the maximum number of charactersto ignore and the termination character. If you write <TT>ignore(80,'\n')</TT>, upto 80 characters will be thrown away until a newline character is found. The newlineis then thrown away and the <TT>ignore()</TT> statement ends. Listing 16.8 illustratesthe use of <TT>ignore()</TT>.</P><P><A NAME="Heading33"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 16.8. Usingignore().</B></FONT></P><PRE><FONT COLOR="#0066FF">1: // Listing 16.8 - Using ignore()2: #include <iostream.h>3:4: int main()5: {6: char stringOne[255];7: char stringTwo[255];8:9: cout << "Enter string one:";10: cin.get(stringOne,255);11: cout << "String one" << stringOne << endl;12:13: cout << "Enter string two: ";14: cin.getline(stringTwo,255);15: cout << "String two: " << stringTwo << endl;16:17: cout << "\n\nNow try again...\n";18:19: cout << "Enter string one: ";20: cin.get(stringOne,255);21: cout << "String one: " << stringOne<< endl;22:23: cin.ignore(255,'\n');24:25: cout << "Enter string two: ";26: cin.getline(stringTwo,255);27: cout << "String Two: " << stringTwo<< endl;28: return 0;<TT>29: }</TT></FONT><FONT COLOR="#0066FF">Output: Enter string one:once upon a timeString oneonce upon a timeEnter string two: String two:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -