|
-
Oct 21st, 2001, 07:35 PM
#1
Thread Starter
Addicted Member
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 -|-
Email: [email protected]
Website: http://www.hurgh.org/
Unix, Linux, FreeBSD, OpenBSD, Solaris, Windows
C, C++, PHP, VB6, ASP, VBScript, JavaScript
-
Oct 21st, 2001, 08:16 PM
#2
Hyperactive Member
Re: Finding out if value is numeric
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?
-
Oct 21st, 2001, 08:23 PM
#3
Thread Starter
Addicted Member
Thanks for the help, just one last question though
-|- Hurgh -|-
Email: [email protected]
Website: http://www.hurgh.org/
Unix, Linux, FreeBSD, OpenBSD, Solaris, Windows
C, C++, PHP, VB6, ASP, VBScript, JavaScript
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|