-
isdigit()
So I have stored a string from the user input, and I want to check whether it is an integer. So it calls my is_digit function, sending it the inputted sting:
Code:
int is_digit(char digit)
{
while(1)
{
if (isdigit(digit))
{
return (digit);
break;
}
else
{
printf("Please reenter value, only numbers please\n");
scanf("%s", digit);
printf("%s", digit);
continue;
}
}
}
so it is meant to check whether digit is a number, if it is send it back else ask for a number, check that is a digit, and if se, send it back
whats wrong with what i wrote above.
please can someone help
Thanks
-
What's wrong with the concept:
It seems like a better idea to call isdigit from your original input function, rather than trying to do input and checking in the same function.
Not strictly wrong, but completely meaningless:
To continue the loop is implied at the end of the loop, and there is nothing to "skip to" at beyond your continue, so you can remove that.
Possibly wrong:
If you have not yet set digit before calling the function, it is incorrect to call isdigit() on the uninitialized value of digit.
Some things that are DIRECTLY wrong in
Using the %s format to read a single character will not work - that format reads a string consisting of zero or more characters, terminating with a zero at the end of the string. You are supplying scanf with a single char - the only valid string that could possibly be stored in one char is an empty string - whch is not a digit. I suggest you use %c instead.
You also do not supply the address of digit, to scanf.
Also beware of the newline - you may want to put a space in your format string.
--
Mats
-
if you want to check a char printf or scan do not use %s but %c. %s is for strings(char*),pointers to char.
If you change %s to %c you check only one char.
TO check a string use char* type and check every char of the string.a char* is an array of chars.
Use strlen(mystring) to get the length of your string and then
use for to check every char of your string like
Code:
char userinput[100];
scanf("%s",userinput);
len=strlen(userinput);
for(int i=0;i<len;i++)
{
if(isdigit(userinput[i])==true)
...
else
...
}
-
I have to scan for 6 integers, all being scanned for as a sting, but need to all be checked to see if they are integers.
Code:
printf("A:");
scanf("%99s", as);
printf("B:");
scanf("%99s", bs);
printf("C:");
scanf("%99s", cs);
printf("Please input Xo, Yo and Zo\n");
printf("\nXo:");
scanf("%99s", Xos);
printf("Yo:");
scanf("%99s", Yos);
printf("Zo:");
scanf("%99s", Zos);
I thought there would be a neater way of checking to see if they are numbers than putting
Quote:
Code:
char userinput[100];
scanf("%s",userinput);
len=strlen(userinput);
for(int i=0;i<len;i++)
{
if(isdigit(userinput[i])==true)
...
else
...
}
after each one.
or not?
-
your function could be like this:
Code:
int is_digit(char* digit)
{
int len=strlen(digit);
for(int i=0;i<len;i++)
{
if(isdigit(digit[i])==false)
{
return -1;
}
}
return atoi(digit);
}
the function get a char*(a string) and checks every char if is digit. If all the chars are digits will return the atoi of the digit(ascii to int value). if found a char that is not digit will return -1 which will mean for your main programm error(a char of the user's input is not digit).
From your main program if you get -1 calling is_digit just ask him for input again.It's good practice not to scanf from is_digit because this function sounds only checking and not for gettting user's input
-
in your main program to get 6 times user's input:
Code:
int res=0;
char user[100];
for(int i=1;i<=6;i++)
{
printf("for i=%d\n",i);
scanf("%s",user);
res=is_digit(user);
if(res==-1)
{
printf("error on input\n");
}
else
{
printf("Ok value is:%d\n",res);
}
}
-
So the program sends the input value to the function is_digit. On its return it checks to see whether it = -1:
Code:
for(i=1;i<=6;i++)
{
if (i=1)
{
//scan for number, send to is_digit
}
else if (i=2)
{
//scan for number, send to is_digit
}
else if (i=3)
{
//scan for number, send to is_digit
}
else if (i=4)
{
//scan for number, send to is_digit
}
else if (i=5)
{
//scan for number, send to is_digit
}
else if (i=6)
{
//scan for number, send to is_digit
}
if(result==-1)
{
printf("One or more entries contained symbols other than numbers. Please re-enter the data\n");
continue;
}
else
{
printf("Ok value is:%d\n",result);
}
}
}
But the for loop doesnt increase i by 1 so it still tries to scan and store in the same spot each time.
also, what if the entered number is a decimal i.e 0.67 or a negative -43
it wont like that will it?
-
Code:
if (i=1)
{
//scan for number, send to is_digit
}
else if (i=2)
{
//scan for number, send to is_digit
}
else if (i=3)
{
//scan for number, send to is_digit
}
else if (i=4)
{
//scan for number, send to is_digit
}
else if (i=5)
{
//scan for number, send to is_digit
}
else if (i=6)
{
//scan for number, send to is_digit
}
1. You have mixed up comparison and assignment operator: compare if equal is ==, make it equal is =.
2. The point of a loop here is that you should be able to walk through your string [which is an array] and pass the current position to isdigit. You should not have a for-loop and then do if(i == x) for every value of the loop - that's completely pointless.
--
Mats
-
My example works only for integers >= 0. if the input number is negative the is_digit will return -1 because of the "-".
It depencne what u want to do and what the program expect from user, u can use convertion functions.
atoi() wiil convert an char* to int - > example atoi("123abcf") will return 123
atoi("absd1234") will return 0
I never tested but i think that atoi will return a negative int if you write something like this atoi("-100")
So, u can use the concertions and then u can check if the (converted) input is that that your program expects
-
strtol give even more feedback
if you pass "123abc" to it, it returns 123 and sets the end pointer to point to "abc",
so checking that something left in the input buffer after converted number you can be sure that the whole buffer is correct or not
-
OK, so i am ammending the function is_digit to allow negative sign as the first symbol, and decimal signs.
Below is just for the decimal sign, but it doesnt work,
Code:
int is_digit(char* digit)
{
int len=strlen(digit);
int i;
for(i=0;i<len;i++)
{
if(isdigit(digit[i])==0)
{
if ((digit[i]) == ".")
{
}
else
{
return -1;
}
}
}
return atoi(digit);
}
can anyone see why?