5

I am trying to write a function that prompts a user for a five digit number, and I want to write an exception block to handle bad input incase the user tries to enter a string or some non-integer input.

I know how to write an exception handling block for something like a division function where you thrown an exception for the denominator being 0, but I have no clue how to do this for input that I have no control over.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
JeramyRR
  • 4,273
  • 3
  • 20
  • 20

3 Answers3

7

First of all, I'd generally advise against this -- bad input from the user is nearly the rule rather than the exception.

If you insist on doing it anyway, you'd probably do it by accepting anything as input, and then throwing an exception when/if it's not what you want:

// Warning: untested code.    
// Accepting negative numbers left as an exercise for the reader.
int get_int(std::istream &is) { 
    std::string temp;

    std::getline(temp, is);

    for (int i=0; i<temp.size(); i++)
        if (!isdigit(temp[i]))
            throw std::domain_error("Bad input");
    return atoi(temp);
}

I repeat, however, that I don't think this is generally a good idea. I'd note (for one example) that iostreams already define a fail bit specifically for situations like this (and the normal operator>> to read an int uses that mechanism).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • You use `atoi()` but still check every character of your string for non-digits. Why?? – vsz Oct 30 '11 at 18:46
  • Thank you. For some reason I was stuck in the mindset that my input had to be stored in an integer variable. Accepting the input as a string and then converting it solves my problem. – JeramyRR Oct 30 '11 at 18:56
  • @vsz: His question was specifically about checking for bad input and throwing an exception when it was encountered. Having done that, `atoi` was the quickest and easiest way to return an int. – Jerry Coffin Oct 30 '11 at 19:19
0

The most straightforward solution is to read a string, and convert it to integer, for example with atoi. If you get a zero as result, and your string was not "0", you know that the input was not numerical, without needing to loop through the string yourself.

vsz
  • 4,811
  • 7
  • 41
  • 78
  • Unfortunately I need to use 0 to terminate the input portion of the program so I need to know when the input is truely 0. – JeramyRR Oct 30 '11 at 18:57
  • You can check separately if the input is really 0 or not. A more detailed discussion can be found at http://stackoverflow.com/questions/1640720/how-do-i-tell-if-the-c-function-atoi-failed-or-if-it-was-a-string-of-zeros – vsz Oct 30 '11 at 19:06
0

Exceptions are often appropriate when accepting data from another machine or process, where there is no reason to expect invalid data to occur within an otherwise valid data stream. With regard to user input, exceptions should only be used in cases which should represent a clear departure from normal program flow. For example, if a program has a tty-style interface, and a routine which is supposed to read a line of input gets a control-C, it may be useful for such a routine to throw an exception. The control-C shouldn't necessarily end the program, but if the program is expecting to read several lines of input from the user, typing control-C at one should frequently cause the remainder to be skipped.

supercat
  • 77,689
  • 9
  • 166
  • 211