>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.
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
Offline 5,055 posts
since Apr 2006