Usage of cin. ignore () and cin. ignore ()
The cin. ignore () function is a method in the C ++ standard input stream (cin. The cin. ignore () function has two parameters: Numeric a and numeric ch, that is, cin. ignore (a, ch ). It indicates that characters are extracted from the input stream cin. Extracted characters are ignored and not used. Every time a character is discarded, it must be counted and compared: If the Count value reaches a or the discarded character is ch, then cin. ignore () function execution ends; otherwise, it continues to wait.
A common function of this function is to clear the content of the input buffer ending with a carriage return and eliminate the impact of the last input on the next input. For example, cin. ignore (1024, '\ n'), usually set the first parameter to be large enough, so that only the second parameter' \ n' works, therefore, this statement clears the characters before the carriage return (including the carriage return) from the input buffer stream.
If no parameter is provided by default, the default parameter is cin. ignore (1, EOF), that is, to clear the first 1 character before EOF. If no EOF is encountered, clear one character and end.
The following example describes the usage of the cin. ignore () function:
For example, we want to accept a string (with no space) and then output it. The C ++ code is as follows:
1 # include <iostream> 2 using namespace std; 3 4 int main () 5 {6 char str [30]; 7 cout <"enter a string :"; 8 cin> str; 9 cout <str <endl; 10 11 return 0; 12}
Run as follows:
When you press any key, the running program exits immediately. Do not quit immediately if you want to stop the program. It is easy to think of letting the program accept another input at the end. After inputting any character, the program exits. Therefore, we add an input (cin. get () to the original code. The Code is as follows:
1 # include <iostream> 2 using namespace std; 3 4 int main () 5 {6 char str [30]; 7 cout <"enter a string :"; 8 cin> str; 9 cout <str <endl; 10 11 cout <"enter any character to end the program! \ N "; 12 cin. get (); 13 14 return 0; 15}
Run the following command:
If you press any key, the program will exit immediately without stopping. This is inconsistent with the expected result. Why?
If you accept another input at the end of the program, that is, add a cin. get () to the code and run it again, for example:
When the character a is entered, the program does not exit immediately, which meets our expectation.
Looking back, why can't I add the first cin. get? It must be because of the first cin. get () gets a character. If you think about it carefully, it is not difficult to think that when you enter "hello" and press the Enter key, the first cin. get () gets '\ n '. Without adding the second cin. get (), we can use the cin. ignore () function to ignore '\ n' extracted from cin. The Code is as follows:
1 # include <iostream> 2 using namespace std; 3 4 int main () 5 {6 char str [30]; 7 cout <"enter a string :"; 8 cin> str; 9 cout <str <endl; 10 11 cin. ignore (100, '\ n'); 12 cout <"enter any character to end the program! \ N "; 13 cin. get (); 14 15 return 0; 16}
Run the following command:
At this time, and add two cin. get () and get the same result. Add cin. ignore (100, '\ n'); the function of this line of code is to ignore the press enter after entering "hello.