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.
Printable View
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.
>> I need a function to detect the A
Use isdigit() to test each character.
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...
>> 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. ;)
I like #2.
2? but then don't try to
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.Code:if ('0' <= c <= '9') ...
isdigit() worked for char...
What if I had
int num1;
and user enter an alphabet for num1?
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.
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?
EDIT: I find this code stupid, because all I know is how to use loops, my teacher is total useless.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;
}
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.
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
Actually, isdigit() is not in the code you posted.
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().