In-depth analysis of cin <, cin. get, cin. getline, and other functions Many beginners think that the cin function is a very simple function! The cin function has a lot of knowledge to know (for example, what is the return value of cin, And what member functions are provided by cin, such as cin. clear (), cin. ignore (), cin. fail (), cin. good () and so on), if you do not have a good grasp of it, you may encounter problems when using it, but you do not know why! In addition, many people have indeed encountered many problems. Below are some simple examples: Procedure 1: # Include <iostream> Using namespace std; Int main () { Int m, n; Cin> m; Cin> n; Return 0; } Test conditions: If you enter two valid numbers each time, the program will not go wrong! However, if you give an invalid input when you enter the 'A' character for the first time, for example, you will find that the program does not The second input statement is executed. It seems a little strange !! Procedure 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 very simple, that is, input a string and then output it again. Program execution: Test 1: Abcd (Press ENTER) Abcd (output) Efgh (Press ENTER) Efgh (output) When the number of characters entered for the first time is less than 4, the program runs normally! Test 2: Abcdefgh (Press ENTER) Abcd (output) (Output-line feed) When the number of characters entered for the first time is greater than 4, the first string accepts the first four characters, the second input operation is not executed, and the second string output is empty. It seems strange !!! In fact, many times you will encounter such problems. If you are not familiar with the program input principle and the principles of some functions such as cin, you do not know how to solve them! I will give a brief introduction here. It may not be very accurate or comprehensive, or there may be some misunderstandings. Please forgive me! Input Operation Principle Like the scanf function mentioned in the previous section, the input of a program has a buffer, that is, the input buffer. The input process is like this. When a keyboard input ends, the input data is stored in the input buffer, and the cin function directly retrieves data from the input buffer. Because the cin function retrieves data directly from the buffer zone, sometimes when there is residual data in the buffer zone, the cin function will directly obtain the residual data without requesting keyboard input, this is why the input statement is invalid in the example! CinSome Input Functions and operators Cin is a extern istream object. Provides many available member functions and heavy-duty operators, such as: cin <, cin. get (), cin. getline () and so on. Let's take a look at these functions: I. cin < This operator reads data based on the type of the following variable. Enter the end condition: Enter, Space, and Tab are displayed. (This is important !) Processing of Terminator: discards the terminator (Enter, Space, Tab) that ends the input in the buffer) Read characters: Procedure 3: # Include <iostream> Using namespace std; Int main () { Char c1, c2; Cin> c1; Cin> c2; Cout <c1 <"" <c2 <endl; Return 0; } Test 1 input: A [Enter] B [Enter] Output: A B Test 2 input: A B [Enter] Output: A B Reading strings: Procedure 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 1 input: Abcd [Enter] Efgh [Enter] Output: Abcd Efgh [Analysis] it is normal to input a carriage return. Test 2 input: Abcd efgh Output: Abcd Efgh [Analysis] When a string is read for the first time, spaces are stopped. abcd is read into str1, spaces are discarded, and the subsequent strings are given to the second string. This proves that the process of reading data from cin ends with a space and discards the space character. There is a residual data room in the buffer zone, and the read operation directly retrieves data from the buffer zone. Ii. cin. get () The function has three formats: No parameter, one parameter, and two parameters. That is, cin. get (), cin. get (char ch), cin. get (array_name, Arsize) Read characters: Enter the end condition: Enter Processing the Terminator: Do not discard the Enter in the buffer Cin. get () and cin. get (char ch) are used to read characters. Their usage is similar, That is, ch = cin. get () is equivalent to cin. get (ch. Procedure 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 values of the two characters Return 0; } Test 1 input: A [Enter] Output: A 97 10 [Analysis] only one input from the keyboard is executed. Obviously, the first character is 'A', and the second variable is Enter (ASCII value: 10 ), this is because this function does not discard the Enter character at the end of the last input, so the buffer left at the end of the first input is the Enter character at the end of the last input! Test 2 input: A B [Enter] Output: A 97 32 [Analysis] Obviously, the first character is 'A', and the second variable is Space (ASCII value: 32 ). The reason is the same as above. The Space character is not discarded. Reading strings: Cin. get (array_name, Arsize) is used to read strings and can accept space characters. When the input ends with Enter, it reads characters according to the length (Arsize) and discards the last Enter character. Procedure 6: # Include <iostream> Using namespace std; Int main () { Char a [20]; Cin. get (a, 10 ); Cout <a <endl; Return 0; } Test 1 input: Abc def [Enter] Output: Abc def [Analysis] indicates that space is acceptable when the function inputs a string. Test 2 input: 1234567890 [Enter] Output: 123456789 [Analysis] If the input is too long, the data is retrieved Based on the required length. Procedure 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 1 input: 12345 [Enter] Output: 1234 53 [Analysis] The first input is too long, and the string is "1234" in length, while the '5' is still in the buffer. Therefore, the second input character is not read from the keyboard, the printed ASCII value is 53 (the ASCII value of '5 ). Test 2 input: 1234 [Enter] A [Enter] Output: 1234 97 [Analysis] The second input is valid, indicating that the function discards the Enter after the first input! Iii. cin. getline () Cin. getline () is similar to cin. get (array_name, Arsize) in Read mode. It ends with Enter and can accept space characters. When reading characters by length (Arsize), the last Enter character is discarded. However, these two functions are different: Cin. get (array_name, Arsize) when the input string is too long, it will not cause the cin function error. Subsequent cin operations will continue, but will retrieve data directly from the buffer zone. However, when the input of cin. getline () is too long, the cin function will be wrong, and subsequent cin operations will not be executed. (The specific cause will be detailed in the next section "Troubleshooting of 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. the get (array_name, Arsize) routine will find that the ch here does not read 5 in the buffer, but returns-52. Here, the cin> ch statement is not executed, this is because the cin has an error! |