943,982 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2130
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 24th, 2007
0

catching errors

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
Mar 24th, 2007
0

Re: catching errors

Input the number as a string, then pop it into a stream and back out to get it back in number form.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. if (convert)
  2. // good
  3. else
  4. // bad
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 24th, 2007
0

Re: catching errors

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
Mar 24th, 2007
0

Re: catching errors

>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:
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 24th, 2007
0

Re: catching errors

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
Mar 24th, 2007
0

Re: catching errors

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:
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 24th, 2007
0

Re: catching errors

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:
C++ Syntax (Toggle Plain Text)
  1. istringstream convert(line)
Does this create a constructor class of istringstream?
What's the terminology for convert?

C++ Syntax (Toggle Plain Text)
  1. convert >> num;
I'm guessing 'convert' with string line parsed in it is converted to and stored in int num;

C++ Syntax (Toggle Plain Text)
  1. if (!convert)
not sure what convert is with the not equal syntax on it.
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
Mar 24th, 2007
0

Re: catching errors

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. #include <string>
for using string in my code or does that somehow get's included when you use:
C++ Syntax (Toggle Plain Text)
  1. #include <sstream>


Last edited by rugae; Mar 24th, 2007 at 2:11 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
Mar 24th, 2007
0

Re: catching errors

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..."
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is online now Online
7,747 posts
since May 2006
Mar 24th, 2007
0

Re: catching errors

>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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 13
Solved Threads: 1
Newbie Poster
ad_rulz is offline Offline
12 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: system("pause");
Next Thread in C++ Forum Timeline: adding and deleting node in a linked list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC