Hmmm...
Lets say we have
If the user input a letter, the problem will crash, of course.Code:int a;;
cout << "Please input a number";
cin >> a;
How do i check if it will happen? So i can do something like: "Please, input the number again".
Printable View
Hmmm...
Lets say we have
If the user input a letter, the problem will crash, of course.Code:int a;;
cout << "Please input a number";
cin >> a;
How do i check if it will happen? So i can do something like: "Please, input the number again".
Always read user input as a string. If it is supposed to be numeric, attempt to convert it. If the conversion fails, reprompt.
I dont know how to check if an string is composed only by numbers...
Are you sure that theres not another way? =~~
>I dont know how to check if an string is composed only by numbers...
http://www.parashift.com/c++-faq-lit....html#faq-39.2
>Are you sure that theres not another way? =~~
There are plenty. I've just tried to shorten your process of learning several dozen things that have issues with them and just get you to the bottom line.
You don't need to. You simply check if the stream is valid.
Code:int a;
cout << "Please input a number";
if(cin >> a) {
// input was successful. a is holding the value
else {
// anything other than an int was introduced
}
Ok, Thank you both =]
If you want to catch things like 123a as a non-number, you may want to review what I mentioned earlier.
The stringstreams have the same issues (and very similar solutions) as just checking cin. I'd still just check cin and add the extra checks to it as necessary.
Yes, but string input makes cleanup easier IMO.