Link to home
Start Free TrialLog in
Avatar of TomG_london
TomG_london

asked on

How so I produce an error message when wrong type of input is entered?

I've created a small text game where the player is expected to provide input of type unsigned short which is read in to a variable using cin.  I want to put in an error handling routine such that if the input is invalid (i.e. a character), it will produce an error message.

Does anyone know how to do this?

Thanks,

Tom
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Try..Catch is probably what you want:

Define the catching routine:

catch (xBoundary err);


Accept the user input:

Input << cin;


Try and convert it:

try
{
  InputValue = atoi (Input);
} catch (xBoundary *err)
  {
    cout << ErrorMessage;
  }



Kdo

Avatar of PlanetCpp
PlanetCpp

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main(void)
{
     int i = 0;

     cout<<"Enter an integer: ";
     cin>>i;
     while(cin.fail())
     {
          cin.clear();
          cin.ignore(INT_MAX,'\n');
          cout<<"invalid input.\n";
          cout<<"Enter an integer: ";
          cin>>i;
     }
     cout<<i<<endl;

     return 0;
}
Avatar of TomG_london

ASKER

kdo, this was the kind of thing I was thinking of, but do you know how to check if the input is of the wrong type, if it helps the function is of the form:

int Get (void)
{
int i;

cout << "Enter an integer" << endl;
cin >> i;

return i;
}

Tom

try/catch will catch any kind of error that you choose.  If:

cin >> IntValue;  /* Sorry -- I type this backwards the first time */

is the only statement in the "try" block, then it is the only thing than can throw the error and you will have trapped the illegal input.  If the input is valid (all numeric) then nothing is thrown and the code continues normally.

If you're using a C++ IDE, check its help on try/catch.  There should be a complete example that you can cut and paste into your code.  The setup and parameters can be a bit confusing which is why I suggest that you start with the example in the help and modify it to fit your needs.


Good Luck!
Kent
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'd like to give points to you both as you have both helped solve the problem, Kdo by providing me with the try block syntax and planetcpp with how to identify the error using cin.fail.  I will give the points to kdo as I was really looking for a solution using try blocks but wasn't sure how to do it.  Thanks both.

BTW do either of you know any other methods for detecting if the input was not of expected type?

Brute force.  Accept a string and parse it yourself.

This is something that you wouldn't do a lot in real life, but it does come up.

You can also accept a string and run it through sscanf().  Though it can be a bit finicky.

This could be a good exercise.  Build your own API than when fed a string and a format template, parses the data according to the template.


Good Luck,
Kdo