Solved

How to trap an error

Posted on 2002-03-17
6
310 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Expert Comment

by:gurpreets
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now