c++ validate integer input code example
Example 1: how to make sure the user inputs a int and not anything else c++
int x;
std::cin >> x;
while(std::cin.fail()) {
std::cout << "Error" << std::endl;
std::cin.clear();
std::cin.ignore(256,'\n');
std::cin >> x;
}
Example 2: integer type validation c++
cout << endl << "Introduce an integer: ";
p = scanf("%d", &x);
getchar();
while (p == false)
{ cout << endl << "You didn't introduce an integger. Try again.";
cout << endl << "Introduce an integer: ";
p = scanf("%d", &x);
getchar();
}