catching errors

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

catching errors

 
0
  #1
Mar 24th, 2007
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?
Last edited by rugae; Mar 24th, 2007 at 12:23 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: catching errors

 
0
  #2
Mar 24th, 2007
Input the number as a string, then pop it into a stream and back out to get it back in number form.
  1. int number;
  2. string line;
  3. getline(cin, line);
  4. istringstream convert(line);
  5. convert >> number;

Check for errors by testing the stream object after conversion.
  1. if (convert)
  2. // good
  3. else
  4. // bad
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

Re: catching errors

 
0
  #3
Mar 24th, 2007
I'm making this calculator thing, my knowledge in c++ isn't very deep at all, 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? I'm confused.
Last edited by rugae; Mar 24th, 2007 at 1:14 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: catching errors

 
0
  #4
Mar 24th, 2007
>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:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. int num=0;
  7.  
  8. while (num != 99) {
  9. cout << "Please enter a number, or 99 to quit." << endl;
  10. cin >> num;
  11. }
  12.  
  13. return 0;
  14. }

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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

Re: catching errors

 
0
  #5
Mar 24th, 2007
That's what happens with my program, when you enter a string when cin type is int, it enters some infinity loop in my while loop. I get that part now, so how do I use the if..else to test for the string and only get the numbers?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: catching errors

 
0
  #6
Mar 24th, 2007
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:
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7. int num=0;
  8. string line;
  9.  
  10. while (num != 99) {
  11. cout << "Please enter a number, or 99 to quit." << endl;
  12. getline(cin, line);
  13. istringstream convert(line);
  14. convert >> num;
  15. if (!convert)
  16. cout << "Error, you must enter a valid number." << endl;
  17. }
  18.  
  19. return 0;
  20. }

It would be trivial to modify this into a loop that keeps waiting until the user enters valid input.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

Re: catching errors

 
0
  #7
Mar 24th, 2007
Thank you joeprogrammer!
Mashed my keyboard... my little calcuation program didn't crash. Perfect!

I do have a few question about istringstream since I don't know what it does:
  1. istringstream convert(line)
Does this create a constructor class of istringstream?
What's the terminology for convert?

  1. convert >> num;
I'm guessing 'convert' with string line parsed in it is converted to and stored in int num;

  1. if (!convert)
not sure what convert is with the not equal syntax on it.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

Re: catching errors

 
0
  #8
Mar 24th, 2007
  1. istringstream convert(line);
  2. convert >> num;
  3. if (!convert)

What would be the pseudocode for the 3 lines above, well I want to understand it as well.

After some reading I found that istringstream is used to capture stirings for using as ints and such so the above would be something like:

using the constructor from istringstream class, content of the string 'line' is copied in to its internally associated string object named 'convert'.
or something like declare string stream 'convert'
'num' reads 'convert' into memory
if not type? 'convert' do ... <- :eek: I don't understand nor have good explanation for !convert?

Another thing is do i need to include the
  1. #include <string>
for using string in my code or does that somehow get's included when you use:
  1. #include <sstream>


Last edited by rugae; Mar 24th, 2007 at 2:11 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: catching errors

 
0
  #9
Mar 24th, 2007
convert is the stringstream object. The convert is true or false based on the execution of the >> operator. Therefore
  1. if (!convert)
actually says "if the previous operation was not successful..."
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 10
Reputation: ad_rulz is an unknown quantity at this point 
Solved Threads: 1
ad_rulz ad_rulz is offline Offline
Newbie Poster

Re: catching errors

 
0
  #10
Mar 24th, 2007
Originally Posted by joeprogrammer View 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 using cin to read in numbers is quite limited. If you have any doubts about it, look at the following program:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. int num=0;
  7.  
  8. while (num != 99) {
  9. cout << "Please enter a number, or 99 to quit." << endl;
  10. cin >> num;
  11. }
  12.  
  13. return 0;
  14. }

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.

I am sorry but when you say that cin has limited capability of reading the numbers what exactly do you mean. And why does it go to the infinite loop. I would be much obliged if you can reply to this as well. Thanks in advance.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC