3, c ++ and get the getline

1, getline: line-oriented input

Read the entire line, a line break by the end of the Enter key to enter the Hey that input.

You may be used cin.getline (name, 20).

The first parameter is the name of the array used to store input rows getline specified number of characters read or encounters stop reading when the line breaks. (Note the difference between cin)

const int size = 20;
char name1[size];
char name2[size];
cout << "enter your name:\n";
cin.getline(name1, size);
cout << "enter your desert:\n";
cin.getline(name2, size);

cout << "I have some delicious " << name2 << " for you," << name1<< endl;	

 

2, get usage

And usage getline substantially the same, but get no line breaks discarded, but leaving it in the input queue.

Because after the first call, a line break is to stay in the input queue, so you see when the second call is the first newline character, then get considered to have reached the end, but did not find anything that can be read.

WITHOUT cin.get parameter () reads the next character, line feed process can be used to prepare the input for the next row is read.

Use cin.get (name1, size); cin.get (); equivalent to cin.get (name1, size) .get ();

3, using the get () benefits

How do you know the reason is to stop reading because they have read the entire line instead of an array to fill it? You can read the next character, if a newline had read the entire row or description of the line there are characters.

4, a dummy read row

When the bit is set invalid after reading the empty row get, the next input block, may be used cin.clear () restore the input.

If the input character string allocated space longer than the getline () and get () will all remaining characters left in the input queue,

And getline () will fail bit set, and closes the input later.

 

Guess you like

Origin www.cnblogs.com/gaoyixue/p/11458416.html