?
Solved

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

Posted on 2003-03-09
7
Medium Priority
?
181 Views
Last Modified: 2010-04-01
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
Comment
Question by:TomG_london
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8098810
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
 
LVL 6

Expert Comment

by:PlanetCpp
ID: 8099713
#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
 

Author Comment

by:TomG_london
ID: 8103521
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8103632

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

Accepted Solution

by:
Kent Olsen earned 60 total points
ID: 8103686
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
 

Author Comment

by:TomG_london
ID: 8106732
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8108057

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question