# [HELP] functions to detect non-numerical

This is a discussion on [HELP] functions to detect non-numerical within the C Programming forums, part of the General Programming Boards category; If I wanted the user to enter 2 numbers to be multiply/add, if user enter something like A5, I need ...

1. ## [HELP] functions to detect non-numerical

If I wanted the user to enter 2 numbers to be multiply/add, if user enter something like A5, I need a function to detect the A, and tell the user non-numerical found. thanks.

2. >> I need a function to detect the A

Use isdigit() to test each character.

3. Time to investigate the ASCII table. You can distinguish characters easily using their numerical value:

char 'a' = 97
char '5' = 53

These are all values between 0 and 127, which is enough to hold both cases of the English alphabet, digits 0-9, punctuation, and a few other things. If you look at it you will be able to see a way to prune non-digits.

I think there is a command too...

4. >> Time to investigate the ASCII table. You can distinguish characters easily using their numerical value:

There are three basic ways to do it:
1) isdigit; Portable. Locale independant.
2) Literal comparison, eg: in the range '0' - '9'; Portable. Locale dependant.
3) 'Magic constant' comparison, eg: in the range 0x30 - 0x39; Non-portable. Locale dependant.

I'd go with #1, personally.

5. I like #2.

6. 2? but then don't try to
Code:
`if ('0' <= c <= '9') ...`
I'd use isdigit, since it is probably the most portable. What if a character set stores '0' as, say, 89 and '1' to '9' as 111 to 119? That's not likely, but it could happen.

7. Originally Posted by Brafil
2? but then don't try to
Code:
`if ('0' <= c <= '9') ...`
I'd use isdigit, since it is probably the most portable. What if a character set stores '0' as, say, 89 and '1' to '9' as 111 to 119? That's not likely, but it could happen.
Actually, it cannot happen for a standard conforming implementation since it is guaranteed that '0' + 1 == '1', '1' + 1 == '2', ..., '8' + 1 == '9'.

8. Originally Posted by laserlight
Actually, it cannot happen for a standard conforming implementation since it is guaranteed that '0' + 1 == '1', '1' + 1 == '2', ..., '8' + 1 == '9'.
OK. The one with the biggest mouth on earth spoke again (-> me). Ignore my post.

9. isdigit() worked for char...

int num1;

and user enter an alphabet for num1?

10. Originally Posted by Dakaa
and user enter an alphabet for num1?
Depends how you are gathering the input. A safe way is to take everything as a string and use strtol(); if you are reading numbers from a text file (an almost unavoidable task), you will have to do that, so you need to learn it sometime.

11. Well you need some what to get the value into num1 in the first place, so you might use scanf et al and check the return value, or fgets and loop throught the string, testing each character with isdigit.

12. my mickey mouse code, trying to use isdigit() to make sure int bal, wit and dep are numbers, so I have to turn bal,wit and dep to char?

Code:
```#include <conio.h>
#include <stdio.h>

int main()
{
int i,bal,wit,dep;
char key[2];

for(;;)
{
bal=0;
wit=0;
dep=0;

for(i=0;i<=2;i++)
{
key[i]='\0';
}

printf("[W/D]? ");
key[i]=getch(); //getche() works as well

if(key[i]=='W'||key[i]=='w')
{
printf("\nenter opening balance: ");
scanf("%i",&bal);
printf("withdraw amount: ");
scanf("%i",&wit);

if((bal-wit)>-201)
{
printf("closing balance: %i\n\n",(bal-wit));
}

else
{
printf("not allowed\n\n"); //no overdraft more than 200
}

}

else if(key[i]=='D'||key[i]=='d')
{
printf("\nenter opening balance: ");
scanf("%i",&bal);
printf("deposit amount: ");
scanf("%i",&dep);
printf("closing balance: %i\n\n",(bal+dep));
}

else
{
printf("\ntry again\n\n");
}

}

getch();
return 0;
}```
EDIT: I find this code stupid, because all I know is how to use loops, my teacher is total useless.

13. I don't think you've quite got what we're saying. Once you say "scanf" you've already lost the battle -- either you've read an int and there's nothing to check, or they typed a character, in which case scanf gave you nothing and you have nothing to check.

You need to read the input as it is, with say fgets, and then check what you've got.

14. Replacing scanf with fgets, it says,

1. invalid conversion from `int*' to `int'

2. too few arguments to function `char* fgets(char*, int, FILE*)'

3. at this point in file

15. Originally Posted by Dakaa
trying to use isdigit() to make sure int bal, wit and dep are numbers, so I have to turn bal,wit and dep to char?
Actually, isdigit() is not in the code you posted.

Originally Posted by tabstop
Once you say "scanf" you've already lost the battle -- either you've read an int and there's nothing to check, or they typed a character, in which case scanf gave you nothing and you have nothing to check.
Beyond that, there is nothing a character can hold that an integer cannot, so you may end up with a value in the int that corresponds to a character (google "ASCII table") and this will still be a "digit". isdigit() would test if that integer ascii value equates to the characters 0-9 (the int value is usually 48-57). This is why I recommended getting the number in a string (eg, "1") and using strtol() or the easier but "less secure" atoi().

Page 1 of 2 12 Last