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??

13th Aug 2017, 6:57 AM
Chang Steve
Chang Steve - avatar
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);
13th Aug 2017, 7:07 AM
Babak
Babak - avatar
+ 1
@Babak Sheykhan (PERS) thanks a lot!!
13th Aug 2017, 8:11 AM
Chang Steve
Chang Steve - avatar