Resetting cin stream state C++

后端 未结 3 994
说谎
说谎 2020-12-04 01:17

Here Im trying to get an integer from user, looping while the input is correct.

After entering non integer value (e.g \"dsdfgsdg\") cin.fail() returns true, as expec

3条回答
  •  萌比男神i
    2020-12-04 01:26

    cin.clear() does not clear the buffer; it resets the error flags. So you will still have the sting you entered in your buffer and the code will not allow you to enter new data until you clear the cin buffer. cin.Ignore() should do the trick

提交回复
热议问题