# Thread: Checking for a number

1. ## Checking for a number

So I have a function that is called everytime the user inputs something, to check whether it is a number or, includes letters aswell. But I want the user to be able to input a decimal, such as 1.234 or 0.4354 etc. I also want the user to be able to input negative numbers, and fractions.

Here is my function at the moment,

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);
}```
It should only be checking to see whether the letter is a decimal point, and if so allow it, but it doesnt work. I am going to also make if statements checking whether the first letter is a - sign and allow that in a similar manner.

Please can someone sport whats wrong.

Thanks

2. single quotes here
Code:
`if ((digit[i]) == ".")`

Code:
```  if ((digit[i]) == '-')//checking whether the first symbol, that is a negative sign
{
}
else
{
return -1;
}```
It worked for the decimal, but not for minus sign for some reason?

4. What's the full code? It looks like you are returning -1 if the first character is not a minus. It could validly be not minus, right?

--
Mats

5. the full loop is:
Code:
```for(i=0;i<len;i++)
{
if(isdigit(digit[i])==0)
{
if (i==0)//checking to see whether first character is 0, before checking if its a minus.
{
if ((digit[i]) == '-')//checking whether the first symbol, that is a negative sign
{
}
else
{
return -1;
}
}

if ((digit[i]) == '.')//checking whether the symbol that is not a number, is a decimal point.
{

}
else
{
return -1;
}
}
}```

6. It looks like you forgot to handle the "normal" cases where the character is a numeral.

7. Code:
```                    if ((digit[i]) == '-')//checking whether the first symbol, that is a negative sign
{
}
else
{
return -1;
}```
Would it not be easier to write (and read):
Code:
```if ((digit[i]) != '-')//checking whether the first symbol, that is a negative sign
{
return -1;
}```
?

--
Mats

8. What's the point in checking for a decimal point if you are converting to an int? Also, how do you know if it's returning "bad negative number encountered", or if they actually entered "-1"?

Quzah.