Solved

How to trap an error

Posted on 2002-03-17
6
315 Views
Last Modified: 2010-04-02
Hi,

I have declared a variable as integer and i make some calculations based on the user input..I have taken care of negative numbers but how to trap if the user enters anything other than a number...

here is the sample code which i have used..

cout << "Choose a option of your choice: \n";
cin >> selection;


     if (!cin)
          cerr << "Invalid selection \n";
     else
     if (selection<0)
          cout << " You have entered a negative selection " << amount << "\n";
     else
     if  (selection>5)
          cout << " Invalid selection: " << amount << "\n";

Thanks

0
Comment
Question by:sramkris
[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
6 Comments
 
LVL 12

Expert Comment

by:lexxwern
ID: 6875963
what you're doing is okay but i just sweetened it a bit with some modifications.

if (!cin || selection<0 || selection>5)
   cout << "ERROR:Invalid Selection \n";
0
 
LVL 1

Author Comment

by:sramkris
ID: 6876114
Hi

This still does not work...If i enter a alphabet or any other key apart from a number it just scrolls or rather hangs...IT does not solve the problem...Is there any way i can force the user to only use numbers...

Thanks
0
 
LVL 3

Expert Comment

by:job_s
ID: 6876191
If Ur using Getchar() U can verify the input by checking the char between 48 - 57

:gch
char ch= Getchar()
if( ch<48 && ch > 57 )
   GoTo gch;

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 2

Expert Comment

by:gurpreets
ID: 6876451
The only way you can check for invalid inputs is by taking char input and converting it to int if it is valid.
0
 
LVL 1

Expert Comment

by:Shivshankar
ID: 6876685
It works very well in my compiler.
Could you pls tell what compiler do u use ?
0
 
LVL 12

Accepted Solution

by:
lexxwern earned 100 total points
ID: 6877116
char ch= Getchar()
if( !(ch<48 && ch > 57) )
{

         cout << " You have entered a negative selection " << amount << "\n";
}

this should do it or you.


lhn
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

623 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