Why is this an infinite loop?

.
Last edited on
Because errorneous input is still in input buffer.

How is that works:
<Output line Enter your PIN Number -->  >
Try to read int;
Hm, input buffer is empty. Request user for input;
<inputted "11">
reading 1... OK, removing it from buffer
reading 1... OK, removing it from buffer
FINISHED. Your number is 11!
Other possibility:
...
<inputted "dd">
reading d... Wait it is not a digit
ACHTUNG!!! Set failed state!!! Stop Processing!!
check if state is failed... true
clear error state.
<Output line Enter your PIN Number -->  >
Try to read int;
There are still input in buffer. Read it first.
reading d... Wait it is not a digit
ACHTUNG!!! Set failed state!!! Stop Processing!!
check if state is failed... true
......... etc.


Use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') after ignore
Last edited on
Clear just clears the error flags. You want to clear the error flags then ignore anything left in the buffer as MiiNiPaa mentioned.
http://www.cplusplus.com/reference/ios/ios/clear/
http://www.cplusplus.com/reference/istream/istream/ignore/
Topic archived. No new replies allowed.