⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch16.htm

📁 一本好的VC学习书,本人就是使用这本书开始学习的vc,希望能对大家有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
	<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>&lt;</TT>) redirect input and (<TT>&gt;</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>&gt;</TT>)and (<TT>&gt;</TT>)redirect input (<TT>&lt;</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>&gt;&gt;</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 &lt;&lt; &quot;Enter a number: &quot;;cin &gt;&gt; someVariable;</FONT></PRE><P>The global object <TT>cout</TT> is discussed later today; for now, focus on thethird line, <TT>cin &gt;&gt; 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>&gt;&gt;</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&amp;</TT>, <TT>short&amp;</TT>,<TT>long&amp;</TT>, <TT>double&amp;</TT>, <TT>float&amp;</TT>, <TT>char&amp;</TT>,<TT>char*</TT>, and so forth. When you write <TT>cin &gt;&gt; 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 &amp; operator&gt;&gt; (int &amp;)</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 &lt;iostream.h&gt;4:5:     int main()6:     {7:        int myInt;8:        long myLong;9:        double myDouble;10:       float myFloat;11:       unsigned int myUnsigned;12:13:       cout &lt;&lt; &quot;int: &quot;;14:       cin &gt;&gt; myInt;15:       cout &lt;&lt; &quot;Long: &quot;;16:       cin &gt;&gt; myLong;17:       cout &lt;&lt; &quot;Double: &quot;;18:       cin &gt;&gt; myDouble;19:       cout &lt;&lt; &quot;Float: &quot;;20:       cin &gt;&gt; myFloat;21:       cout &lt;&lt; &quot;Unsigned: &quot;;22:       cin &gt;&gt; myUnsigned;23:24:       cout &lt;&lt; &quot;\n\nInt:\t&quot; &lt;&lt; myInt &lt;&lt; endl;25:       cout &lt;&lt; &quot;Long:\t&quot; &lt;&lt; myLong &lt;&lt; endl;26:       cout &lt;&lt; &quot;Double:\t&quot; &lt;&lt; myDouble &lt;&lt; endl;27:       cout &lt;&lt; &quot;Float:\t&quot; &lt;&lt; myFloat &lt;&lt; endl;28:       cout &lt;&lt; &quot;Unsigned:\t&quot; &lt;&lt; myUnsigned &lt;&lt; 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 &quot;kinds&quot;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 &lt;&lt; &quot;Enter your name: &quot;;cin &gt;&gt; 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 &quot;endof string&quot; to the standard library functions discussed on Day 21, &quot;What'sNext.&quot;<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 &lt;iostream.h&gt;4:5:     int main()6:     {7:        char YourName[50];8:        cout &lt;&lt; &quot;Your first name: &quot;;9:        cin &gt;&gt; YourName;10:       cout &lt;&lt; &quot;Here it is: &quot; &lt;&lt; YourName &lt;&lt; endl;11:       cout &lt;&lt; &quot;Your entire name: &quot;;12:       cin &gt;&gt; YourName;13:       cout &lt;&lt; &quot;Here it is: &quot; &lt;&lt; YourName &lt;&lt; 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 &lt;iostream.h&gt;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 &lt;&lt; &quot;int: &quot;;15:       cin &gt;&gt; myInt;16:       cout &lt;&lt; &quot;Long: &quot;;17:       cin &gt;&gt; myLong;18:       cout &lt;&lt; &quot;Double: &quot;;19:       cin &gt;&gt; myDouble;20:       cout &lt;&lt; &quot;Float: &quot;;21:       cin &gt;&gt; myFloat;22:       cout &lt;&lt; &quot;Word: &quot;;23:       cin &gt;&gt; myWord;24:       cout &lt;&lt; &quot;Unsigned: &quot;;25:       cin &gt;&gt; myUnsigned;26:27:       cout &lt;&lt; &quot;\n\nInt:\t&quot; &lt;&lt; myInt &lt;&lt; endl;28:       cout &lt;&lt; &quot;Long:\t&quot; &lt;&lt; myLong &lt;&lt; endl;29:       cout &lt;&lt; &quot;Double:\t&quot; &lt;&lt; myDouble &lt;&lt; endl;30:       cout &lt;&lt; &quot;Float:\t&quot; &lt;&lt; myFloat &lt;&lt; endl;31:       cout &lt;&lt; &quot;Word: \t&quot; &lt;&lt; myWord &lt;&lt; endl;32:       cout &lt;&lt; &quot;Unsigned:\t&quot; &lt;&lt; myUnsigned &lt;&lt; endl;33:34:       cout &lt;&lt; &quot;\n\nInt, Long, Double, Float, Word, Unsigned: &quot;;35:       cin &gt;&gt; myInt &gt;&gt; myLong &gt;&gt; myDouble;36:       cin &gt;&gt; myFloat &gt;&gt; myWord &gt;&gt; myUnsigned;37:       cout &lt;&lt; &quot;\n\nInt:\t&quot; &lt;&lt; myInt &lt;&lt; endl;38:       cout &lt;&lt; &quot;Long:\t&quot; &lt;&lt; myLong &lt;&lt; endl;39:       cout &lt;&lt; &quot;Double:\t&quot; &lt;&lt; myDouble &lt;&lt; endl;40:       cout &lt;&lt; &quot;Float:\t&quot; &lt;&lt; myFloat &lt;&lt; endl;41:       cout &lt;&lt; &quot;Word: \t&quot; &lt;&lt; myWord &lt;&lt; endl;42:       cout &lt;&lt; &quot;Unsigned:\t&quot; &lt;&lt; myUnsigned &lt;&lt; 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 &quot;word&quot;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, &quot;How does the extractionoperator manage this trick of concatenation?&quot;<H4 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">operator&gt;&gt;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 &lt;&lt; &quot;Enter three numbers: &quot;cin &gt;&gt; VarOne &gt;&gt; varTwo &gt;&gt; varThree;</FONT></PRE><P>When you write <TT>cin &gt;&gt; VarOne &gt;&gt; varTwo &gt;&gt; varThree;</TT>,the first extraction is evaluated <TT>(cin &gt;&gt; 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 &gt;&gt; varOne) &gt;&gt; varTwo) &gt;&gt; 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&gt;&gt;</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&gt;&gt;</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 + -