Cout Press the Enter Key to Continue Endl cin ignore cin get
The cin.ignore() function is a method in the C++ standard input stream (cin). There are two parameters in the cin.ignore() function, which are numeric a and character ch, namely cin.ignore( a, ch ). It means that characters are extracted from the input stream cin, and the extracted characters are ignored and not used. And every time a character is discarded, it must count and compare characters: if the count value reaches a or the discarded character is ch, the cin.ignore() function execution terminates; otherwise, it continues to wait.
One of its common functions is to clear the contents of the input buffer that ends with a carriage return and eliminate the influence of the previous input on the next input. For example, it can be used like this, cin.ignore(1024,'\n'), usually the first parameter is set large enough, so that actually only the second parameter'\n' works, so this sentence is All characters before the carriage return (including carriage return) are removed from the input buffer stream.
If no parameter is given by default, the default parameter is cin.ignore(1, EOF), that is, clear the 1 character before EOF, and clear one character before EOF is encountered and then end.
Here is an example to briefly illustrate the usage of cin.ignore() function:
For example, we ask to accept input of a string (without spaces), and then output the string. The C++ code is as follows:
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 char str[30 ]; 7 cout << " Please enter a string: " ; 8 cin >> str; 9 cout << str << endl; 10 11 return 0 ; 12 }
Run as shown below:
When any key is pressed, the running program will exit immediately. If you want the program to stay for a while, do not exit immediately. We can easily think of accepting another input at the end of the program, and the program exits after entering any character. Therefore, we add an input to the original code, namely cin.get(), the code is as follows:
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 char str[30 ]; 7 cout << " Please enter a string: " ; 8 cin >> str; 9 cout << str << endl; 10 11 cout << " Please enter any character to end the program! \n " ; 12 cin.get (); 13 14 return 0 ; 15 }
At this time, the operation is as follows:
Pressing any key will still exit the program without stopping, which is inconsistent with the result we want. Why is this?
If you accept one more input at the end of the program, add another cin.get() to the code, and run the following figure again:
When the character a is input at this time, the program does not exit immediately, which meets our expected requirements.
Thinking back, why didn't adding the first cin.get() work? It must be because the first cin.get() gets a certain character. If you think about it carefully, it is not difficult to think that when you press the Enter key after typing "hello", the first cin.get() gets it. It is'\n'. Without adding the second cin.get(), we can think of using the cin.ignore() function to ignore the'\n' extracted from cin. The specific code is as follows:
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 char str[30 ]; 7 cout << " Please enter a string: " ; 8 cin >> str; 9 cout << str << endl; 10 11 cin.ignore(100, ' \n ' ); 12 cout << " Please enter any character to end the program! \n " ; 13 cin.get (); 14 15 return 0 ; 16 }
Run as follows:
At this time, the same result is obtained as adding two cin.get(), adding cin.ignore(100,'\n'); the function of this line of code is to ignore the carriage return after entering "hello" Drop.
this usage
Preface: This parameter is a parameter of a function, which is very important in object-oriented programming. In JavaScript, its value depends on the function call mode, and the this point may be diff...
The usage of for in and in
1.For...In statement is used to loop/iterate the properties of arrays or objects (enumerable, that is, including the properties in the instance, and the enumerable properties in the prototype). For ar...
Usage of $this
The meaning of $this is to indicate The specific object after instantiation! We generally declare a class first, and then use this class to instantiate the object! However, when we declare this class,...
~ Usage
Everyone knows that when connecting to the access database in vs2005, you often need to configure the path in webconfig <appSettings> <!--Connect to the ACCESS databas...
THIS usage
Three effects in Java: Difference to global variables and local variables 2. THIS in the method indicates that the current object calls the method, this means the current object 3. THIS () in the cons...
Usage of "~"
Usage of "~" Didn't understand, still study ...
Usage
Class and object usage First, the basic use of the class Class included: Static field (static variable), dynamic field (dynamic variable) and constructive method The static field is written before __i...
Source: https://programmerall.com/article/5959670213/
0 Response to "Cout Press the Enter Key to Continue Endl cin ignore cin get"
Enregistrer un commentaire