Ok, I new to this and haven't been taught exception handling yet but I would like my program to run without crashing when someone mashes the keyboard. I have declared an int x, and I don't want my program to crash when user inputs an char or more than 10 digits... what should I do?
rugae
0
Light Poster
Recommended Answers
Jump to PostInput 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 …
Jump to Post>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 usingcin
to read in numbers is quite limited. If you have any doubts about it, look at the following program:
Jump to PostWell, 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 …
All 10 Replies
John A
1,896
Vampirical Lurker
Team Colleague
rugae
0
Light Poster
John A
1,896
Vampirical Lurker
Team Colleague
rugae
0
Light Poster
John A
1,896
Vampirical Lurker
Team Colleague
rugae
0
Light Poster
rugae
0
Light Poster
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
ad_rulz
3
Newbie Poster
John A
1,896
Vampirical Lurker
Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.