Possible Duplicate:
how do I validate user input as a double in C++?
Hi there. How do I make it so the user can only input integers/floats without listing a giant switch statement or something?
Thanks.
Possible Duplicate:
how do I validate user input as a double in C++?
Hi there. How do I make it so the user can only input integers/floats without listing a giant switch statement or something?
Thanks.
int i;
std::cin >> i;
if(!std::cin) throw "bloody user blew it!"
That's some of the most fundamental stuff you learn about C++. You might want to get a good introductory C++ book.
while (std::cin.fail()) { std::cout << "Bad input, try again"; std::cin >> i;}
. My key points being (a) don't use exceptions since the OP might be a student just learning the basics and (b) use fail() explicitly rather than showing how to test the stream directly.
– Vatsan
Nov 02 '10 at 22:49