Program skips cin.getline()

前端 未结 5 1802
野的像风
野的像风 2020-12-19 12:18

I have made this program, It get the users adress, name and work. Then It puts it all into one string and outputs that string. (I know there are better ways to do this)

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 12:31

    cin.getline(name,10); reads at most NINE characters from the input, stopping earlier if there's a newline. If there's no newline in those 9 characters, it won't touch the rest of the line, and the next call to getline will continue reading the rest of the line.

    If you want to just ignore the rest of the input line, you could instead use:

    cin.get(name, 10);
    cin.ignore(INT_MAX, '\n');
    

提交回复
热议问题