| | |
catching errors
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 27
Reputation:
Solved Threads: 0
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.
Input the number as a string, then pop it into a stream and back out to get it back in number form.
Check for errors by testing the stream object after conversion.
C++ Syntax (Toggle Plain Text)
int number; string line; getline(cin, line); istringstream convert(line); convert >> number;
Check for errors by testing the stream object after conversion.
C++ Syntax (Toggle Plain Text)
if (convert) // good else // 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.
All my posts may be freely redistributed under the terms of the MIT license.
>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
Try running it and see what happens when you type in some letters instead of numbers...
Hopefully you see my point now.
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)
#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. "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.
All my posts may be freely redistributed under the terms of the MIT license.
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:
It would be trivial to modify this into a loop that keeps waiting until the user enters valid input.
C++ Syntax (Toggle Plain Text)
#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.
"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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Mar 2007
Posts: 27
Reputation:
Solved Threads: 0
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:
Does this create a constructor class of istringstream?
What's the terminology for convert?
I'm guessing 'convert' with string line parsed in it is converted to and stored in int num;
not sure what convert is with the not equal syntax on it.
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)
istringstream convert(line)
What's the terminology for convert?
C++ Syntax (Toggle Plain Text)
convert >> num;
C++ Syntax (Toggle Plain Text)
if (!convert)
•
•
Join Date: Mar 2007
Posts: 27
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
istringstream convert(line); convert >> num; 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)
#include <string>
C++ Syntax (Toggle Plain Text)
#include <sstream>
Last edited by rugae; Mar 24th, 2007 at 2:11 pm.
convert is the stringstream object. The convert is true or false based on the execution of the >> operator. Therefore c Syntax (Toggle Plain Text)
if (!convert)
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Aug 2006
Posts: 10
Reputation:
Solved Threads: 1
•
•
•
•
>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 usingcinto read in numbers is quite limited. If you have any doubts about it, look at the following program:
C++ Syntax (Toggle Plain Text)
#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.cinfails 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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: system("pause");
- Next Thread: adding and deleting node in a linked list
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






