When I enter the correct value (integer), thatโs good. But when I enter the symbol, I get an infinite loop. I looked at all sides of this code and could not find a problem with it. Why is this happening? I am using g ++ 4.7 on Windows.
#include <iostream> #include <limits> int main() { int n; while (!(std::cin >> n)) { std::cout << "Please try again.\n"; std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cin.clear(); } }
Input: x
Output:

source share