Questions about C ++, using the getline function to get automatically skipped cin stream

Questions about C ++, using the getline function to get automatically skipped cin stream

In some examples of their own work, they found that if used in the code above getline cin stream to get through keyboard input, getline will skip.
Here Insert Picture Description
Here Insert Picture Description

Baidu found after searching waited a long time, a character before the cin stream will take a carriage return after the read data is then transmitted to the carriage return buf cin remaining in the stream, so when getline cin stream when reading The first reading will be followed by a carriage return and exit.

The solution is simple:
use cin.ignore before getline () function to clear the carriage cin residue.
Its default parameters: cin.ignore (1, EOF);
you want to go look up detailed analysis of Baidu.
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/u013594490/article/details/92779827