Solved

How to trap an error

Posted on 2002-03-17
6
314 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
Technology Partners: 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 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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

739 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