getline () and get () (c ++ Learning Notes)

istream classes (e.g. CIN) provides a number of row-oriented class member functions: getline () and get ()

1.getline () function

Read the entire line, a line feed input end of the input to determine the Enter key.

Calling the method: cin.getline (parameter 1, parameter 2)

This function takes two parameters, the first parameter is used to store the name of the array input lines, the second parameter is the number of characters to read, if this parameter is 20, the function reads up to 19 characters, the remaining storage space for the null character is automatically added at the end.

getline () member function stops reading when reading a specified number of characters or a line break.

It is determined by the end of the line line breaks, line breaks without saving the contrary, when the character string is stored, it is replaced with a null newline character.

2.get () function

Calling the method: cin.get (parameter 1, parameter 2)

And getline () as receiving the same parameters, the same parameter interpreted, and are read into the end of the line, but will not get read and discard line breaks, but leaving it in the input queue.

例:cin.get(name,Size);

       cin.get(dessert,Size);

Because after the first call, the line breaks will stay in the input queue, so you see when the second call is the first newline character, so get () considered to have reached the end of the line, but did not find any readable take content.

cin.get available with no argument () reads the next character (even line feed), and therefore can use it to handle line breaks, ready for the next row is read.

Two members of the class may also be spliced ​​together using, as cin.get (name, Size) .get ();

When using the get () to read the entire line, how do you know the reason is to stop reading because they have read the entire line, and not due to an array of fill?

View the next character, if a newline, already read the entire line, otherwise, indicating the bank as well as other inputs.

Guess you like

Origin www.cnblogs.com/zhi321/p/11427290.html