Finding out if value is numeric
Hi,
Just wondering, In c, if i use this code snippit:
Code:
int value;
printf("Please enter a value: ");
scanf("%d", &value);
how can i then check that value is a numeric value, i know that by declaring it as int it should be numeric, but i have made a menu system that dispalys some options and the user has to enter a number that currosponds to the selection they want to use.
This works great if the user enters a number, but if a user hits a letter key by mistake, then the menu is just displayed over and over again, without stopping.
Maybe i need a way to allow the user to only enter numbers and no letters, is that a possibility, and if so, could you please post some sample code for me (the code needs to be in c, and will be used in a console app)..
Thanks for your help.
-|- Hurgh -|-
Re: Finding out if value is numeric
Quote:
Originally posted by hurgh
Hi,
Just wondering, In c, if i use this code snippit:
Code:
char value;
printf("Please enter a value: ");
scanf("%c", &value);
if(value>=48 && value<=57) //48 to 57 is ascii number for 1-9
{
value=value-48;
}
else
{
cout<<"a alphabet is entered, program exits."<<endl;
return;
}
//do what you want hereafter
The above is kedaman's method. I think is about time I collect some advertising fees from kedaman.
hurgh, if you want to use 10 or above, try to figure out yourself, it is not difficult, ok?