tags: C++

Action executed in 0.000

Each Tag

C++

Common tags - number of posts

input operator (1), programming (1), _answer (1), infinite loop (1), number (1),

infinite loop answer

After a little research i found this web page.

  • If std::cin is presented with input it cannot process, std::cin goes into a "fail" state
  • The input it cannot process is left on the input stream.

So "asdf" is not valid integer input. It is left on the input stream. Next time we read, same thing occurs.

What's the best resolution? After cin >> Command we can check the fail flag in cin with this if (!cin) { cin.clear(); cin.ignore( numeric_limits<streamsize>::max(), '\n' ); } then reprompt.

parent post: code snippet
notify me: yes

code snippet

I'm having trouble seeing why that'd be an infinite loop. Would the cin >> Command; not re-prompt the user after some non-number is entered the same as it would after some non-6 number is entered?
parent post: Extended Kilmer Day
notify me: yes