📄 ch16.htm
字号:
<DD><HR><FONT COLOR="#000077"><B>New Term: </B></FONT><I>Redirecting</I> refers to sending output (or input) to a place different than the default. The redirection operators for DOS and UNIX are (<TT><</TT>) redirect input and (<TT>></TT>) redirect output. <HR></DL><P>Piping refers to using the output of one program as the input of another.</P><P>DOS provides rudimentary redirection commands, such as redirect output (<TT>></TT>)and (<TT>></TT>)redirect input (<TT><</TT>). UNIX provides more advanced redirectioncapabilities, but the general idea is the same: Take the output intended for thescreen and write it to a file, or pipe it into another program. Alternatively, theinput for a program can be extracted from a file rather than from the keyboard.</P><P>Redirection is more a function of the operating system than of the <TT>iostream</TT>libraries. C++ just provides access to the four standard devices; it is up to theuser to redirect the devices to whatever alternatives are needed.<H3 ALIGN="CENTER"><A NAME="Heading14"></A><FONT COLOR="#000077">Input Using cin</FONT></H3><P>The global object <TT>cin</TT> is responsible for input and is made availableto your program when you include <TT>iostream.h</TT>. In previous examples, you usedthe overloaded extraction operator (<TT>>></TT>) to put data into your program'svariables. How does this work? The syntax, as you may remember, is the following:</P><PRE><FONT COLOR="#0066FF">int someVariable;cout << "Enter a number: ";cin >> someVariable;</FONT></PRE><P>The global object <TT>cout</TT> is discussed later today; for now, focus on thethird line, <TT>cin >> someVariable;</TT>. What can you guess about <TT>cin</TT>?</P><P>Clearly it must be a global object, because you didn't define it in your own code.You know from previous operator experience that <TT>cin</TT> has overloaded the extractionoperator (<TT>>></TT>) and that the effect is to write whatever data <TT>cin</TT>has in its buffer into your local variable, <TT>someVariable</TT>.</P><P>What may not be immediately obvious is that <TT>cin</TT> has overloaded the extractionoperator for a great variety of parameters, among them <TT>int&</TT>, <TT>short&</TT>,<TT>long&</TT>, <TT>double&</TT>, <TT>float&</TT>, <TT>char&</TT>,<TT>char*</TT>, and so forth. When you write <TT>cin >> someVariable;</TT>,the type of <TT>someVariable</TT> is assessed. In the example above, <TT>someVariable</TT>is an integer, so the following function is called:</P><PRE><FONT COLOR="#0066FF">istream & operator>> (int &)</FONT></PRE><P>Note that because the parameter is passed by reference, the extraction operatoris able to act on the original variable. Listing 16.1 illustrates the use of <TT>cin</TT>.</P><P><A NAME="Heading15"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 16.1. cin handlesdifferent data types.</B></FONT></P><PRE><FONT COLOR="#0066FF">1: //Listing 16.1 -- character strings and cin2:3: #include <iostream.h>4:5: int main()6: {7: int myInt;8: long myLong;9: double myDouble;10: float myFloat;11: unsigned int myUnsigned;12:13: cout << "int: ";14: cin >> myInt;15: cout << "Long: ";16: cin >> myLong;17: cout << "Double: ";18: cin >> myDouble;19: cout << "Float: ";20: cin >> myFloat;21: cout << "Unsigned: ";22: cin >> myUnsigned;23:24: cout << "\n\nInt:\t" << myInt << endl;25: cout << "Long:\t" << myLong << endl;26: cout << "Double:\t" << myDouble << endl;27: cout << "Float:\t" << myFloat << endl;28: cout << "Unsigned:\t" << myUnsigned << endl;29: return 0;<TT>30: }</TT></FONT><FONT COLOR="#0066FF">Output: int: 2Long: 70000Double: 987654321Float: 3.33Unsigned: 25Int: 2Long: 70000Double: 9.87654e+08Float: 3.33Unsigned: 25</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis</B></FONT><FONT COLOR="#000000"><B>: </B></FONT>Onlines 7-11, variables of various types are declared. On lines 13-22, the user isprompted to enter values for these variables, and the results are printed (using<TT>cout</TT>) on lines 24-28.</P><P>The output reflects that the variables were put into the right "kinds"of variables, and the program works as you might expect.<H4 ALIGN="CENTER"><A NAME="Heading17"></A><FONT COLOR="#000077">Strings</FONT></H4><P><TT>cin</TT> can also handle character pointer (<TT>char*</TT>) arguments; thus,you can create a character buffer and use <TT>cin</TT> to fill it. For example, youcan write this:</P><PRE><FONT COLOR="#0066FF">char YourName[50]cout << "Enter your name: ";cin >> YourName;</FONT></PRE><P>If you enter <TT>Jesse</TT>, the variable <TT>YourName</TT> will be filled withthe characters <TT>J, e, s, s, e, \0</TT>. The last character is a null; <TT>cin</TT>automatically ends the string with a null character, and you must have enough roomin the buffer to allow for the entire string plus the null. The null signals "endof string" to the standard library functions discussed on Day 21, "What'sNext."<H4 ALIGN="CENTER"><A NAME="Heading18"></A><FONT COLOR="#000077">String Problems</FONT></H4><P>After all this success with <TT>cin</TT>, you might be surprised when you tryto enter a full name into a string. <TT>cin</TT> believes that white space is a separator.When it sees a space or a new line, it assumes the input for the parameter is complete,and in the case of strings it adds a null character right then and there. Listing16.2 illustrates this problem.</P><P><A NAME="Heading19"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 16.2. Tryingto write more than one word to cin.</B></FONT></P><PRE><FONT COLOR="#0066FF">1: //Listing 16.2 -- character strings and cin2:3: #include <iostream.h>4:5: int main()6: {7: char YourName[50];8: cout << "Your first name: ";9: cin >> YourName;10: cout << "Here it is: " << YourName << endl;11: cout << "Your entire name: ";12: cin >> YourName;13: cout << "Here it is: " << YourName << endl;14: return 0;<TT>15: }</TT></FONT><FONT COLOR="#0066FF">Output: Your first name: JesseHere it is: JesseYour entire name: Jesse LibertyHere it is: Jesse</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis</B></FONT><B>: </B>On line 7, a character arrayis created to hold the user's input. On line 8, the user is prompted to enter onename, and that name is stored properly, as shown in the output.</P><P>On line 11, the user is again prompted, this time for a full name. <TT>cin</TT>reads the input, and when it sees the space between the names, it puts a null characterafter the first word and terminates input. This is not exactly what was intended.</P><P>To understand why this works this way, examine Listing 16.3, which shows inputfor a number of fields.</P><P><A NAME="Heading21"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 16.3. Multipleinput.</B></FONT></P><PRE><FONT COLOR="#0066FF">1: //Listing 16.3 - character strings and cin2:3: #include <iostream.h>4:5: int main()6: {7: int myInt;8: long myLong;9: double myDouble;10: float myFloat;11: unsigned int myUnsigned;12: char myWord[50];13:14: cout << "int: ";15: cin >> myInt;16: cout << "Long: ";17: cin >> myLong;18: cout << "Double: ";19: cin >> myDouble;20: cout << "Float: ";21: cin >> myFloat;22: cout << "Word: ";23: cin >> myWord;24: cout << "Unsigned: ";25: cin >> myUnsigned;26:27: cout << "\n\nInt:\t" << myInt << endl;28: cout << "Long:\t" << myLong << endl;29: cout << "Double:\t" << myDouble << endl;30: cout << "Float:\t" << myFloat << endl;31: cout << "Word: \t" << myWord << endl;32: cout << "Unsigned:\t" << myUnsigned << endl;33:34: cout << "\n\nInt, Long, Double, Float, Word, Unsigned: ";35: cin >> myInt >> myLong >> myDouble;36: cin >> myFloat >> myWord >> myUnsigned;37: cout << "\n\nInt:\t" << myInt << endl;38: cout << "Long:\t" << myLong << endl;39: cout << "Double:\t" << myDouble << endl;40: cout << "Float:\t" << myFloat << endl;41: cout << "Word: \t" << myWord << endl;42: cout << "Unsigned:\t" << myUnsigned << endl;43:44:45: return 0;<TT>46: }</TT></FONT><FONT COLOR="#0066FF">Output: Int: 2Long: 30303Double: 393939397834Float: 3.33Word: HelloUnsigned: 85Int: 2Long: 30303Double: 3.93939e+11Float: 3.33Word: HelloUnsigned: 85Int, Long, Double, Float, Word, Unsigned: 3 304938 393847473 6.66 bye -2Int: 3Long: 304938Double: 3.93847e+08Float: 6.66Word: bye</FONT></PRE><PRE><FONT COLOR="#0066FF">Unsigned: 65534</FONT></PRE><P><FONT COLOR="#000077"><B>Analysis</B></FONT><FONT COLOR="#000000"><B>:</B></FONT><B></B>Once again, a number of variables are created, this time including a <TT>char</TT>array. The user is prompted for input and the output is faithfully printed.</P><P>On line 34, the user is prompted for all the input at once, and then each "word"of input is assigned to the appropriate variable. It is in order to facilitate thiskind of multiple assignment that <TT>cin</TT> must consider each word in the inputto be the full input for each variable. If <TT>cin</TT> was to consider the entireinput to be part of one variable's input, this kind of concatenated input would beimpossible.</P><P>Note that on line 35 the last object requested was an <TT>unsigned</TT> integer,but the user entered <TT>-2</TT>. Because <TT>cin</TT> believes it is writing toan <TT>unsigned</TT> integer, the bit pattern of <TT>-2</TT> was evaluated as an<TT>unsigned</TT> integer, and when written out by <TT>cout</TT>, the value <TT>65534</TT>was displayed. The <TT>unsigned</TT> value <TT>65534</TT> has the exact bit patternof the <TT>signed</TT> value <TT>-2</TT>.</P><P>Later in this chapter you will see how to enter an entire string into a buffer,including multiple words. For now, the question arises, "How does the extractionoperator manage this trick of concatenation?"<H4 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">operator>>Returns a Reference to an istream Object</FONT></H4><P>The return value of <TT>cin</TT> is a reference to an <TT>istream</TT> object.Because <TT>cin</TT> itself is an <TT>istream</TT> object, the return value of oneextraction 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 valuefrom this is another <TT>istream</TT> object, and that object's extraction operatorgets 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 Functionsof cin</FONT></H3><P>In addition to overloading <TT>operator>></TT>, <TT>cin</TT> has a numberof 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 CharacterInput</FONT></H4><P><TT>operator>></TT> taking a character reference can be used to get a singlecharacter from the standard input. The member function <TT>get()</TT> can also beused to obtain a single character, and can do so in two ways. <TT>get()</TT> canbe used with no parameters, in which case the return value is used, or it can beused with a reference to a character. Using get() with No Parameters The first formof <TT>get()</TT> is without parameters. This returns the value of the characterfound, 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 concatenatethis 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -