Created
October 26, 2017 16:31
-
-
Save Lhadalo/fd5d4fea62756b930aeb66e9bdd9eedd to your computer and use it in GitHub Desktop.
Prevent the user to input values of incorrect type
This file contains hidden or 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 nbr_elements; | |
std::cin >> nbr_elements; | |
while (std::cin.fail()) | |
{ | |
std::cin.clear(); | |
std::cin.ignore(1000, '\n'); | |
std::cout << "Bad input. Enter a number: " | |
std::cin >> nbr_elements; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment