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
TomG_londonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
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

0
PlanetCppCommented:
#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;
}
0
TomG_londonAuthor Commented:
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
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Kent OlsenData Warehouse Architect / DBACommented:

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
0
Kent OlsenData Warehouse Architect / DBACommented:
Sorry.  I thought that I was running short of time here.

Generally, you just need to expand your code to resemble:

catch (xBoundary err);

int Get (void)
{
  int i;

  try
  {
    cout << "Enter an integer" << endl;
    cin >> i;
  } catch (xBoundary *err)
    {
      cout << ErrorMessage;
      i = 0;
    }

  return i;
}


If you enter a valid integer, the function will return its value.  If you enter an illegal value, an error message will be displayed and 0 returned.  Of course, you could also loop until a valid integer is entered, giving you the same functionality as in the "while" loop in PlanetCpp's example.


Kdo
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TomG_londonAuthor Commented:
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?
0
Kent OlsenData Warehouse Architect / DBACommented:

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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.