The problem is that:
cin >> input;
Will cause the wrong bit when you try to read a non-numeric value. After that, any attempt to use operator>> ignored.
So, the way to fix this is to check if the thread is in good condition, and if not, reset the status flags and try and read it again. But note that bad input (which caused the problem) is still at the input, so you need to also make sure you throw it away too.
if (cin >> input) {
To prevent a jam (caused by the wrong bit), read the line, then use the string stream to analyze user input. I also prefer this method (for user interactive input), because it allows you to simplify the combination of different reading styles (i.e. combine operator>> and std::getline() , as you can use them in a string stream).
#include <iostream>
source share