You can use bool cin.fail() to check if cin >> x was successfull (it won't be for non-integers, since x is int).
Look up cin.clear and cin.ignore functions, as these will come in handy when dealing with looped cin statement.
You can use ignore with parameters specifying how many character to ignore or at which particular character to stop ignoring:
- std::numeric_limits<std::streamsize>::max() - is formal way of saying "As much as it fits inside cin buffer"
- '\n' - is read as: "or when encounter newline" - which happens after pressing Enter.
In full, the line looks like: std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');