1.1.4 getline and cin

When using cin, only the next token is read in. So if you enter a number or word, then press the enter or return key, just the number or word is read in. cin leaves the carriage return/enter part of the line.

When using getline, everything up to the next carriage return/enter is read.

The issue is that if you use a cin and leave only the carriage return/enter, then use a getline, you will pick up just that carriage return/enter and nothing more.

To get around this, you need to clear the remainder of the input line after a cin before you call the getline.

You can do this using the cin.ignore() command.

This will clear the remainder of the line and allow the getline command to wait for the next line.

Try the example with and without the cin.ignore() command to notice the issue.

Last updated