For a scholar who has just been familiar with C ++, the power of C ++ makes you sigh and have a headache ---- it means that the knowledge we need is endless! Therefore, you must master certain learning methods and take notes to remind yourself, or provide some help to beginners! Here are some notes about C ++ primer plues!
To thoroughly learn the C ++ input method, you must first have a certain understanding of the process of the input method. The input method is in the form of a stream, that is: the I/O device first enters the input from the keyboard into a buffer to form an input queue. When the language in your program responds to it, then extract the stream from its input queue one by one or one row. To understand the following input process, you must have a certain understanding of the process and be familiar with its working principles!
1. For the input of CIN, first let's look at the following program:
# Include "iostream"
# Include "string"
Const int size = 20;
Int main ()
{
Using namespace STD;
Char A [size];
Cout <"enter a string :";
Cin> char;
Cout <"the string is:" <char <Endl;
Return 0;
}
The above is a simple CIN input application. When we enter "aaaa", the result is obviously "aaaa", but when we input "Aa aa", it outputs "AA", but why not "aaaa ?! This is because CIN only reads one word when obtaining character arrays. When the CIN input encounters blank spaces, spaces, tabs, and line breaks, it determines the character string bounds, the input "Aa aa" above, when reading the first two, the third is a space, so CIN stops reading the RST stream in the input queue, so only output "AA"!
2. Differences between cin. Get () and CIN. geiline () Inputs
For the above input problems, we can solve the problem through the input methods of CIN. Get () and CIN. Getline (). Both methods are row-oriented input methods and modify the above program:
# Include "iostream"
# Include "string"
Const int size = 20;
Int main ()
{
Using namespace STD;
Char A [size];
Cout <"enter a string :";
Cin. Get (A, size); // or CIN. Getline (A, 20 );
Cout <"the string is:" <char <Endl;
Return 0;
}
Both methods are row-oriented input methods. There are two parameters. The first parameter is used to store the array name of the input row, and the second parameter is the number of characters to read, when they use the Enter key or the number of words required to reach the primary stream, they will stop reading the primary stream from the input queue! Therefore, the two input methods meet the conditions and the output result is "Aa aa!
Here, we will first summarize these three methods: CIN only reads the RST Stream one by one from the input queue, while cin. get () and CIN. getline () is a row-oriented input. It reads a row in the input queue! Note the two differences!
Let's take a look at the following program:
# Include "iostream"
Const int size = 20;
Int main ()
{
Using namespace STD;
Char name [size];
Char sex [10];
Cout <'Please enter your name :';
Cin> name;
Cout <"Please enter your sex :";
Cin> sex;
Cout <"Your name is:" <name <Endl;
Cout <"your sex is:" <sex <Endl;
Return 0;
}
When you run the above program, the result is not as you think. The output is as follows:
Please enter your name:;
Please enter your sex:
Your name is:
Your sex is:
........
It can be found that when there is no space in one of your input characters, this program outputs the same as you think, but when there is space, the output is different! Why ?!
This is because when the first input is completed, the space characters are retained in the input queue. When the second input is to be read, the space characters are read, it ends at the end by default, and no content can be input, so the second input is not read!
When I change CIN back to CIN. getline () and CIN. when you get (), you will find cin. get () and CIN have the same results, but cin. getline () is successful. Why !? This is because of CIN and CIN. the get () input method is to keep the linefeed in the input queue, while the cin. getline () does not retain its linefeed in the input queue. If no help is used, CIN and CIN. get () will be difficult to cross the line break, the difference is here! This must also be noted!
Is there any way to solve this problem ?!
You can use this input method to change the program: cin. get (). get () this is because of CIN. get () will return a CIN object, so this is equivalent to executing the CIN twice. get (), or you can. add a CIN after the get () method. get (), that is: CIN> name; cin. get (); or CIN. get (A, 20); cin. get ();. This method works because cin. Get () can read the next character (including line breaks), so you can use it to process line breaks and prepare for reading the next input!
The correct procedure is as follows:
# Include "iostream"
Const int size = 20;
Int main ()
{
Using namespace STD;
Char name [size];
Char sex [10];
Cout <'Please enter your name :';
Cin> name;
Cin. Get ();
Cout <"Please enter your sex :";
Cin> sex;
Cout <"Your name is:" <name <Endl;
Cout <"your sex is:" <sex <Endl;
Return 0;
}
The above are just some of my superficial opinions. I hope you can give me more advice on the learning process!