DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   error-handling (http://www.daniweb.com/forums/thread158945.html)

akira_shinizaki Nov 22nd, 2008 11:42 am
error-handling
 
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

ArkM Nov 22nd, 2008 12:22 pm
Re: error-handling
 
You must detect bad input condition then handle it. If possible, make recovery. For example:
    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'
    }
Of course, it's an example only. Try it then improve. There are lots of methods to cope with i/o errors...

Lerner Nov 22nd, 2008 1:02 pm
Re: error-handling
 
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.

akira_shinizaki Nov 22nd, 2008 2:36 pm
Re: error-handling
 
How I can convert it and know limit between numerical and string value ?

ArkM Nov 22nd, 2008 3:03 pm
Re: error-handling
 
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 ;)...

Lerner Nov 22nd, 2008 4:32 pm
Re: error-handling
 
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.

akira_shinizaki Nov 22nd, 2008 10:06 pm
Re: error-handling
 
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 >.<

Lerner Nov 22nd, 2008 11:16 pm
Re: error-handling
 
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.

ArkM Nov 23rd, 2008 1:28 am
Re: error-handling
 
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.


All times are GMT -4. The time now is 6:09 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC