About using CIN
Cin. Get () getchar (), the terminator of Getline Input
Input values of CIN. Get (), getchar, Cin. Getline
1. How to handle the first character encountered by these functions
Cin. Get ()
# Include <iostream>
# Include <string>
Using namespace STD;
Int main ()
{
Char;
Char B;
A = getchar ();
B = cin. Get ();
Cout <A <B <Endl;
System ("pause ");
Return 0;
}
Input: A + space + B + press ENTER
Output
Input: A + press ENTER
It can be seen that the character B is blank or a carriage return.
Conclusion: When cin. Get () encounters a space at the beginning or press enter, it reads and assigns it to B.
Getchar ()
Replace the statements A and B with the following statements:
Char A, B;
A = getchar ();
B = getchar ();
Input: A + space + B + press ENTER
Input: A + press ENTER
It can be seen that the character B is blank or a carriage return.
Conclusion: getchar () is the same as cin. Get (). If a space at the beginning or press enter is used, it is directly read and assigned to B.
CIN
Replace the statements A and B with the following statements:
Char A, B;
A = getchar ();
Cin> B;
Input: A + space + B + press ENTER
Input: A + press ENTER + B + press ENTER
Conclusion: CIN can skip the leading space and press Enter.
Getline
Replace the statements A and B with the following statements:
Char;
String B
A = getchar ();
Getline (CIN, B );
Input: A + space + B + press ENTER
Input: A + press ENTER
Conclusion: Getline reads the leading space and carriage return. However, when it reads the carriage return, it is not assigned to B, but as the end identifier of B.
2. How to handle the terminator of these characters
String A, B, C;
Cin>;
B = getchar ();
Cout <A <B <Endl;
Input: A + press ENTER
Conclusion: The end character of CIN is the carriage return, which is left in the output queue and read by the following gechar. The Terminator will be retained in addition to Getline.
String A, B, C;
Getline (CIN, );
B = getchar ();
Cout <A <B <Endl;
Input: A + press ENTER + B + press ENTER
Conclusion: The end operator '/N' of Getline is discarded and not retained in the input queue.
To solve the unwanted characters left in the queue, we can solve this problem in two ways:
The first is to eat this character and call cin. Get () once ()
The second is to call cin. Ignore ();