Solved

How to trap an error

Posted on 2002-03-17
6
312 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

774 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