0
Deal with wrong input
what should I write if I want user to input over and over again until I get a proper input?? for example , I want an int, but user give me a char, and I want the program keep asking for input of right type. What should I write??
2 Antworten
+ 5
In C++ you would use a code block like this to handle type errors
int num = 0;
do{
std::cout << "Enter a number: ";
std::cin >> num;
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(200, '\n');
std::cout << "Invalid input!\n";
}
else
break;
} while (true);
+ 1
@Babak Sheykhan (PERS)
thanks a lot!!