error-handling

Reply

Join Date: Oct 2008
Posts: 11
Reputation: akira_shinizaki is an unknown quantity at this point 
Solved Threads: 0
akira_shinizaki akira_shinizaki is offline Offline
Newbie Poster

error-handling

 
0
  #1
Nov 22nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: error-handling

 
0
  #2
Nov 22nd, 2008
You must detect bad input condition then handle it. If possible, make recovery. For example:
  1. int x;
  2.  
  3. cout << "Type integer: ";
  4. if (cin >> x) {
  5. cout << "Thank you..." << endl;
  6. } else if (cin.eof()) { // end of stream
  7. cout << "cin closed." << endl;
  8. } else { // fail state (not a number}
  9. cin.clear(); // reset stream fail bit
  10. cout << "It's not a number." << endl;
  11. cin.ignore(1000,'\n'); // skip upto '\n'
  12. }
Of course, it's an example only. Try it then improve. There are lots of methods to cope with i/o errors...
Last edited by ArkM; Nov 22nd, 2008 at 12:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: error-handling

 
0
  #3
Nov 22nd, 2008
One of the other approaches is to never accept input as a numerical type. Only accept input as a string. Then parse the string after input for it's validity as a numeric value and convert the string to the numerical type as desired if it's valid.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: akira_shinizaki is an unknown quantity at this point 
Solved Threads: 0
akira_shinizaki akira_shinizaki is offline Offline
Newbie Poster

Re: error-handling

 
0
  #4
Nov 22nd, 2008
How I can convert it and know limit between numerical and string value ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: error-handling

 
0
  #5
Nov 22nd, 2008
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 ...
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: error-handling

 
0
  #6
Nov 22nd, 2008
While it's perfectly legitimate to parse the input string with a stringstream and the >> operator, it is not mandatory. Manual parsing will work. It may be tedious, but it will work.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 11
Reputation: akira_shinizaki is an unknown quantity at this point 
Solved Threads: 0
akira_shinizaki akira_shinizaki is offline Offline
Newbie Poster

Re: error-handling

 
0
  #7
Nov 22nd, 2008
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 >.<
Last edited by akira_shinizaki; Nov 22nd, 2008 at 10:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: error-handling

 
0
  #8
Nov 22nd, 2008
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: error-handling

 
0
  #9
Nov 23rd, 2008
Some addition to the wonderful Lerner's explanation.
As usually, closed user dialogue stream treated as the end of interaction condition (for example, the user press Ctrl+Z on Windows). That's why no stream recovery for eof case in my snippet.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC