Input the number as a string, then pop it into a stream and back out to get it back in number form.
int number;
string line;
getline(cin, line);
istringstream convert(line);
convert >> number;
Check for errors by testing the stream object after conversion.
if (convert)
// good
else
// bad
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>I don't understand why you need to convert the int to string to test the int and see if it's a string or not?
Because using cin to read in numbers is quite limited. If you have any doubts about it, look at the following program:
#include <iostream>
using namespace std;
int main() {
int num=0;
while (num != 99) {
cout << "Please enter a number, or 99 to quit." << endl;
cin >> num;
}
return 0;
}
Try running it and see what happens when you type in some letters instead of numbers...
Hopefully you see my point now. cin fails to read, and then everything screws up. Using strings avoids that problem, because the user can enter just about anything and they won't screw up the input buffer. Then you can manually validate the input yourself.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Well, if you take the examples from my first post, and implement them in the program I just showed you, you end up with something like this:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int num=0;
string line;
while (num != 99) {
cout << "Please enter a number, or 99 to quit." << endl;
getline(cin, line);
istringstream convert(line);
convert >> num;
if (!convert)
cout << "Error, you must enter a valid number." << endl;
}
return 0;
}
It would be trivial to modify this into a loop that keeps waiting until the user enters valid input.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
convert is thestringstream object. The convert is true or false based on the execution of the >> operator. Therefore
if (!convert)
actually says "if the previous operation was not successful..."
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
>Another thing is do i need to include the #include <string> for using string in my code
It would be a good idea, yes, although I think the basic string object is included when you use iostream in your program.
>when you say that cin has limited capability of reading the numbers what exactly do you mean.
It's difficult to use it to read in numbers failproof, and plus cin has problems of its own . Using getline is a far better alternative.
>And why does it go to the infinite loop.
2 reasons:Since the user has typed letters, they go into the input buffer. When you try to read them into a number, cin gets an error and "freezes up".
The letters keep sitting in the input buffer, so even if there weren't any errors, cin wouldn't allow the user to enter anything because there's already junk in the input buffer.
You can fix these 2 problems by using cin.clear() to get rid of the errors, and then using cin.ignore() to clear the input buffer. The order is important! If you try to clear the input buffer first, it will fail, because cin still has errors and is "frozen".
#include <iostream>
using namespace std;
int main() {
int num=0;
while (num != 99) {
cout << "Please enter a number, or 99 to quit." << endl;
cin >> num;
if (!cin) {
cout << "Error, you must enter a valid number." << endl;
cin.clear();
}
// we could have put this inside the if() statement, but it's still
// a good idea to clear the buffer, as cin leaves newlines behind
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return 0;
}
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339