Skip to content

Instantly share code, notes, and snippets.

@talllguy
Last active December 30, 2015 18:19
Show Gist options
  • Save talllguy/7867177 to your computer and use it in GitHub Desktop.
Save talllguy/7867177 to your computer and use it in GitHub Desktop.
cin validation loop
int x; // example input variable
cout << "Enter x: "
// Get the input and validate it, check if outside of range
while ((!(cin >> x)) || /* && could be used here too, on nothing else */ (x < 999))
{
cin.clear(); // Clear the error flags
cin.ignore(100, '\n'); // Remove unwanted characters from buffer
cout << "\aEnter x: "; // Sound alert and re-issue the prompt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment