Cin<<, Cin.get,cin.getline and other functions in-depth analysis Many beginners think that the CIN function is a very simple function, not really! The CIN function has a lot of knowledge to understand (for example, what the return value of CIN is, what member functions CIN provides and what the roles are, such as cin.clear (), Cin.ignore (), Cin.fail (), Cin.good (), and so on), If not well mastered, in the use of the time is likely to be a problem but do not know why! And a lot of people are actually experiencing a lot of problems, here are a few simple examples: Program 1: #include <iostream> using namespace Std; int main () { int m, n; cin>>m; cin>>n; return 0; } Test situation: If the user enters two legal number each time, the program does not have the problem! However, if the user gives an illegal input for the first time, such as entering a character ' a ', you will find that the program does not The second input statement is executed again. It seems a little strange!! Program 2: #include <iostream> using namespace Std; int main () { Char Str[8]; Cin.getline (str, 5); cout<<str<<endl; Cin.getline (str, 5); cout<<str<<endl; return 0; } The function of the program is simple, just enter a string and then output again, enter a string output again. Implementation of the program: Test One: ABCD (carriage return) ABCD (output) EFGH (carriage return) EFGH (Output) When the number of characters entered by the user for the first time is less than 4 o'clock, the program executes normally! Test Two: ABCDEFGH (carriage return) ABCD (output) (Output-line wrapping) When the user first enters a number of characters that are greater than 4 o'clock, the first string accepts the first four characters of the input, and the second input operation is not executed, and the second string output is empty. It seems strange too!!! In fact, in many cases will encounter such problems, if not familiar with the principle of program input and CIN and other functions of the principle of not know how to solve! I am here to do a simple introduction, perhaps the introduction is not very accurate and comprehensive, or there are some misunderstandings, please forgive us! How the input operation works As with the scanf function mentioned in the previous section, the input of the program is built into a buffer, which is the input buffer. The first input process is this, when the input data is stored in the input buffer at the end of the keyboard input, and the CIN function takes the data directly from the input buffer. Because the CIN function takes data directly from the buffer, sometimes when there is residual data in the buffer, the CIN function directly obtains the residual data without requesting the keyboard input, which is why the input statement fails in the example! Cin some of the input functions and operators CIN is a extern istream object. Provides a number of available member functions and overloaded operators, such as: Cin<<, Cin.get (), Cin.getline (), and so on. Let's take a look at some of these functions: I. cin<< The operator reads the data according to the type of the subsequent variable. Enter end condition: Encounter Enter, Space, TAB key. (This is important!) ) Handling of Terminator: Discard Terminator (Enter, Space, TAB) in the buffer that makes the input end The case of reading characters: Program 3: #include <iostream> using namespace Std; int main () { Char C1, C2; cin>>c1; cin>>c2; cout<<c1<< "" <<c2<<endl; return 0; } Test one input: A[enter] B[enter] Output: A b Test two inputs: A b[enter] Output: A b The case of reading a string: Program 4: #include <iostream> using namespace Std; int main () { Char str1[10], str2[10]; cin>>str1; cin>>str2; cout<<str1<<endl; cout<<str2<<endl; return 0; } Test one input: Abcd[enter] Efgh[enter] Output: Abcd Efgh The "parse" input encounters a carriage return end, which is normal. Test two inputs: ABCD EFGH Output: Abcd Efgh Parsing reads a string the first time it encounters a space, it stops, it reads ABCD into STR1, and discards the space, giving the next string a second string. This proves that CIN reads the data to the end of the space, and discards the whitespace, the buffer has a residual data room, and the read-in operation takes the data directly from the buffer. Two Cin.get () The function has three formats: no parameter, one parameter, two parameters i.e. Cin.get (), Cin.get (char ch), Cin.get (Array_Name, Arsize) To read a character: Enter end condition: Enter key Processing for Terminator: Do not discard the enter in the buffer Cin.get () and Cin.get (char ch) are used to read characters, their use is similar, That is, Ch=cin.get () is equivalent to Cin.get (CH). Program 5: #include <iostream> using namespace Std; int main () { Char C1, C2; Cin.get (C1); Cin.get (C2); cout<<c1<< "" <<c2<<endl; Print two characters cout<< (int) c1<< "" << (int) c2<<endl; Print the ASCII value of these two characters return 0; } Test one input: A[enter] Output: A 97 10 Analysis will find that only one time from the keyboard input, obviously the first character variable takes the ' a ', the second variable takes the ENTER (ASCII value is 10), because the function does not discard the last input at the end of the Enter character, So at the end of the first input, the buffer remains the enter character at the end of the last input! Test two inputs: A b[enter] Output: A 97 32 "Analysis" Obviously the first character variable takes the ' a ', the second variable takes space (ASCII value is 32). For the same reason, the space character is not discarded. To read a string: Cin.get (Array_Name, arsize) is used to read a string, can accept a space character, encounters a enter end input, reads a character by length (arsize), and discards the last enter character. Program 6: #include <iostream> using namespace Std; int main () { Char a[20]; Cin.get (A, 10); cout<<a<<endl; return 0; } Test one input: ABC Def[enter] Output: ABC def Analysis indicates that the function can accept spaces when it enters a string. Test two inputs: 1234567890[enter] Output: 123456789 The analysis input is long, and the data is taken at the desired length. Program 7: #include <iostream> using namespace Std; int main () { Char ch, a[20]; Cin.get (A, 5); cin>>ch; cout<<a<<endl; cout<< (int) ch<<endl; return 0; } Test one input: 12345[enter] Output: 1234 53 "Analysis" The first time the input is very long, the string by the length of "1234", and ' 5 ' is still left in the buffer, so the second input character is not read from the keyboard, but directly take the ' 5 ', so the printed ASCII value is 53 (' 5 ' ASCII value). Test two inputs: 1234[enter] A[enter] Output: 1234 97 "Analysis" The second input is valid, indicating that the function of the first input after the Enter discarded! Three Cin.getline () Cin.getline () and Cin.get (Array_Name, arsize) read the same way, ending with enter, you can accept the space character. Reading a character by length (arsize) discards the last enter character. But there are differences between the two functions: Cin.get (Array_Name, arsize) when the input string is very long, it does not cause an error in the CIN function, and subsequent CIN operations continue, just fetching data directly from the buffer. However, cin.getline () will cause an error in the CIN function when the input is very long, and the subsequent CIN operation will no longer execute. (Specific reasons are described in detail in the next section, "Error handling in CIN") Program 8: #include <iostream> using namespace Std; int main () { Char ch, a[20]; Cin.getline (A, 5); cin>>ch; cout<<a<<endl; cout<< (int) ch<<endl; return 0; } Test input: 12345[enter] Output: 1234 -52 "Analysis" and Cin.get (Array_Name, arsize) of the routine comparison will find that the CH is not read in the buffer 5, but return 52, here in fact the CIN>>CH statement is not executed, because CIN error! |