Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

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
0
TomG_london
Asked:
TomG_london
  • 4
  • 2
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now