Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

type conversion & error trapping

Status
Not open for further replies.

tetsuya

Technical User
Jul 7, 2010
2
JP
I am working with a console app using c++, and I'm using integers for the data type. I would like to know how to trap an error if the user enters an alpha character instead. I have tried using the if statment below, but the program goes crazy anyway. Does anyone know a way to trap this type of potential error?

TIA, Tetsuya

if(isdigit(input))
code....

 
You could also use:
...
if (( int(input) >= 48) && ( int(input) <= 57)) //checks whether it is in the range of 0..9
// your code
else
cout<<&quot;Invalid format&quot;;
...
or
...
if (isdigit(input))
// your code
else
cout<<&quot;Invalid format&quot;;
Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
You can ignore noon digit characters like below:
int x=0;
istrstream st;
while(x!=13)//press enter to finish loop
{
x=getch();
if(isdigit(x))//other symbols are ignored
{
st<<(char)x;
}
if(x==0)//checking functional keys
{
getch();//eliminating the second symbol from the
//keyboard buffer
}
}
 
Thanks to all who applied. I never thought of scanning for 0-9. The second reply is equally interesting.

Thanks again.

Tetsuya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top