| | |
error-handling
![]() |
•
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
Hallo everyone, I have some problems with my assignment. I asked to provide some error-handling in my program. What I want to ask abou how we handle an input that isn't its type. For example i define a variable int a but we "accidentally" input a string or other type. How we can handle that error type ? Thank you before
You must detect bad input condition then handle it. If possible, make recovery. For example:
Of course, it's an example only. Try it then improve. There are lots of methods to cope with i/o errors...
C++ Syntax (Toggle Plain Text)
int x; cout << "Type integer: "; if (cin >> x) { cout << "Thank you..." << endl; } else if (cin.eof()) { // end of stream cout << "cin closed." << endl; } else { // fail state (not a number} cin.clear(); // reset stream fail bit cout << "It's not a number." << endl; cin.ignore(1000,'\n'); // skip upto '\n' }
Last edited by ArkM; Nov 22nd, 2008 at 12:25 pm.
Please, don't divert your attention. I have presented the proper solution of your problem. Lerner's suggestion is not totally different approach: you need to handle stringstream input errors when process previously accepted string.
Can you differ is it a taste of a fish or a bread? If you get a string - it's a string, if you get a number - it's a numerical value
...
Can you differ is it a taste of a fish or a bread? If you get a string - it's a string, if you get a number - it's a numerical value
... •
•
Join Date: Oct 2008
Posts: 11
Reputation:
Solved Threads: 0
ArKM your code is work better, thank you^^. But I have some code that i don't understand.
} else if (cin.eof()) { //<== what this code mean ?
cout << "cin closed." << endl;
else { // fail state (not a number}
cin.clear(); // reset stream fail bit <== clear() and ignore() ?
cout << "It's not a number." << endl;
cin.ignore(1000,'\n'); // skip upto '\n'
Ah yes, how to make so we can back inputing the data after we handling the error ?
Thank you for your help >.<
} else if (cin.eof()) { //<== what this code mean ?
cout << "cin closed." << endl;
else { // fail state (not a number}
cin.clear(); // reset stream fail bit <== clear() and ignore() ?
cout << "It's not a number." << endl;
cin.ignore(1000,'\n'); // skip upto '\n'
Ah yes, how to make so we can back inputing the data after we handling the error ?
Thank you for your help >.<
Last edited by akira_shinizaki; Nov 22nd, 2008 at 10:13 pm.
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
Once you've determined the stream is in an unusable state you can try to figure out why. One of the reasons is that the end of file has been reached. You can check for that by called the eof() method of istreams. eof() will evaluate to true if end of file has been reached. You can clear the end of file flag with the clear() function. Then you can reuse the stream again. If the stream is unusable for some other reason than end of file, you can output a notice to the user, clear the fail bit with clear(), ignore whatever is in the stream and then reuse it again.
If you wrap all of this in a loop with a sentinnel flag as the conditional then you can change the value of the flag to false if valid input achieved to break out of the loop. Otherwise the loop keeps going until valid input is achieved.
If you wrap all of this in a loop with a sentinnel flag as the conditional then you can change the value of the flag to false if valid input achieved to break out of the loop. Otherwise the loop keeps going until valid input is achieved.
Klatu Barada Nikto
![]() |
Similar Threads
- how to make error handling (VB.NET)
- Error handling (PHP)
- Error Handling in CFSCRIPT (ColdFusion)
- Handling errors in php! ??? (PHP)
- Compilation Error Handles cmdLogin.ServerClick (ASP.NET)
- Another mysql_num_rows(): error (PHP)
- Keyboard Error Handling (C++)
Other Threads in the C++ Forum
- Previous Thread: Creating webpage through Dev C++
- Next Thread: Table
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






