Cin.getline () and cin.get () are read-oriented rows-by-line reads, that is, reading the entire row at a time instead of a single digit or character, but there is a certain difference between the two.
Cin.get () reads one whole line at a time and leaves a newline character generated by the ENTER key in the input queue, for example:
Copy Code code as follows:
#include <iostream>
Using Std::cin;
Using Std::cout;
const int SIZE = 15;
int main () {
cout << "Enter Your name:";
Char Name[size];
Cin.get (name,size);
cout << "Name:" << name;
cout << "\nenter your address:";
Char Address[size];
Cin.get (address,size);
cout << "Address:" << address;
}
Output:
Enter your name:jimmyi shi
Name:jimmyi Shi
Enter your address:address:
In this example, Cin.get () reads the entered name into name and leaves the line feed "/n" generated by enter in the input queue (that is, the input buffer), so the next Cin.get () is found in the buffer. n ' and read it, resulting in a second inability to enter and read the address. The solution is to call Cin.get () after the first call to the Cin.get () to read the '/n ' character, which can be combined to write as Cin.get (name,size).
Cin.getline () reads an entire row at a time and discards the newline character generated by the ENTER key, such as:
Copy Code code as follows:
#include <iostream>
Using Std::cin;
Using Std::cout;
const int SIZE = 15;
int main () {
cout << "Enter Your name:";
Char Name[size];
Cin.getline (name,size);
cout << "Name:" << name;
cout << "/nenter your address:";
Char Address[size];
Cin.get (address,size);
cout << "Address:" << address;
}
Output:
Enter your name:jimmyi shi
Name:jimmyi Shi
Enter your Address:yn QJ
Address:yn QJ
Because the newline character generated by enter is discarded, the next cin.get () read to the address is not affected.
If Cin.get () is read in one character, but Cin.get () does not ignore any characters, the return character needs to be handled separately.
Two points Note:
(1) Learn to distinguish between get () and getline ();
(2) The newline symbol is \ n, not/n;