Correct use of getline
1. String input
1.1 common errors and causes
#include<iostream> const int SIZE=50; //Array length using namespace std; int main() { char name[SIZE]; //name int age; //Age cout << "Enter your name:" <<endl; cin >> name; //Enter name cout << "Enter your age:" <<endl; cin >> age; //Enter age return 0; }
This is a code snippet to get the age and name. When you enter the following, the program runs successfully.
But this is only because the loopholes in the program are cleverly covered up by input. When the name input is no longer "Daming" but "Li Daming", the program ends ahead of time before the input age.
The reason is that cin uses white space (space, tab, carriage return) as the end flag, which means that cin can only get one word when getting the string.
In the actual example, cin enters "Li" into name and then "Daming" into age, but because age is an integer variable, the input fails, and then the program ends.
1.2 solutions
getline()
The getline() function reads the entire line function, determines the end of the input through the line feed, and then discards the line feed.
Calling method: CIN getline(). This function has two parameters. The first is used to store the array name, and the second is used to read the characters and numbers. Note that if the second parameter is 50, enter up to 49 characters, leaving one for the end '\ 0'
get()
There are several variants of this function, one of which is similar to getline(). They accept the same parameters and interpret the parameters in the same way. They all read to the end of the line. But instead of reading and discarding the newline, get() leaves the newline in the input queue.
char name1[SIZE], name2[SIZE]; cin.get(name1, SIZE); cin.get(name2, SIZE);
In this case, the newline character is left in the input queue. What you see in the second call is a newline character. get() thinks it has reached the end of the line, but does not find the content that should be read in.
At this point, you can use another variant of get(). Use CIN get() reads the next character, which handles line breaks in preparation for reading the next line.
cin.get(name1, SIZE); cin.get(); cin.get(name2, SIZE); perhaps cin.get(name1, SIZE).get(); cin.get(name2, SIZE);
get() makes error checking easier. How do you know that the reason for stopping reading is because the whole row has been read, not because the array is full? You can check the next character. If it is a newline character, it means that the whole line has been read. Otherwise, it means that there are other inputs in the line.
Other issues
Blank line: when get() reads a blank line, the invalid bit will be set.
cin.clear();// If the input is blocked, you can use this command to restore
The input string is larger than the allocated space: getline() and get() leave the remaining characters in the input queue, getline() sets the invalid bit and closes the input. 1.3 correct code form:
#include<iostream> const int SIZE=50; //Array length using namespace std; int main() { char name[SIZE]; //name int age; //Age cout << "Enter your name:" <<endl; cin.getline(name, SIZE); //Enter the name or CIN get(name, SIZE). get(); cout << "Enter your age:" <<endl; cin >> age; //Enter age return 0; }
2. string class input
To use the string class, you must first include the header file string. The string class not only realizes the function of character array, but also makes the operation of string more simple, convenient and safe.
Getline (CIN, STR) / / enter a line into a string object
If you want to cycle through multiple sets of information.
#include<iostream> #include<string> using namespace std; struct member // { string name; // int age; // }; int main() { member* d = new member[50]; //Declares a pointer to an array of structures int amount=0; //Number of you want to enter cin>>amount; for(int i=0; i<amount; i++) { cin.get(); //Since getline() is preceded by a number, an empty get() gets the preceding blank getline(cin,d[i].name); cin>>d[i].age; } return 0; }