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".
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.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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 }
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
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.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
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.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*