How to reset std::cin when using it?

Solution 1:

You could use, when the condition, std::cin.fail() happens:

std::cin.clear();
std::cin.ignore();

And then continue with the loop, with a continue; statement. std::cin.clear() clears the error flags, and sets new ones, and std::cin.ignore() effectively ignores them (by extracting and discarding them).

Sources:

  1. cin.ignore()
  2. cin.clear()