Last active
December 30, 2015 18:19
-
-
Save talllguy/7867177 to your computer and use it in GitHub Desktop.
cin validation loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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