check if the input is a number or not

I want to check the user's input if his input is a float or integer number so accept it and do some mathematics operations on it otherwise if his input is characters, symbols, massive number or anything else but number ask him to enter another input. Hint I'm using the data type float for "p.burst_time". but every time when I enter any input the program considers it as a wrong input as the checker becomes = 1 even the input is correct & I don't know why. And thanks in advance.

for(;;){    
int i,checker,point=0;
    char c[25];
    c[strlen(c)-1] = '\0';
printf("\nEnter The Process's Burst Time > 0 : ");
fgets(c, 21, stdin); //Max input is 20.
fflush(stdin);
for(i=0;i<strlen(c);i++) //strlen(c) is the size. Max is 20.
    {

       if(c[i]=='.')
            point++;
       else if(!isdigit(c[i])){
            checker=1; //Checker is 1 when input isn't a digit.
            break;
       }

    }
    printf("checker = %d\npoint = %d\n",checker,point);
    if(checker==1||point>1){
        printf("\a\aPlease enter a positive number only greater than zero.\n"); //Input has space, symbols, letters. Anything but digits.
        continue;
    }
    else
    {
        p.burst_time = atof(c); //Converting to float only if input is nothing but digits.
        if(p.burst_time<=0)
            continue;
        else
            break;
    }
   }

Upvotes: 0

Views: 90

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 148880

Some problems :

  • you never set checker to 0 (only point), so after first error all inputs will be rejected
  • fgets gives you the \n : you should ignore it and initial and terminal white spaces (' ', \r, \t, \f, at least the 2 first)
  • fflush on an input stream is non standard and as you use fgets is useless
  • c[strlen(c)-1] = '\0'; has no sense : you try to put a null to terminate the array, but strlen only gives the position of the first null which is undifined behavious on an unitialized array (thanks to WhozCraig for noticing it) : you could do c[0] = '\0';, or c[sizeof(c) - 1] = '\0'; but here again it is useless

I did not try to run it, so I do not know if there are still others ...

Upvotes: 1

igon
igon

Reputation: 3046

You should use strtod or related functions strtof:

#include<stdio.h>
#include <stdlib.h>

int main() {
    double d;
    char c[25];
    char * converted;
    for(;;){
        printf("\nEnter The Process's Burst Time > 0 : ");
        fgets(c, 21, stdin); //Max input is 20.                                                                                     
        d = strtod(c,&converted);
        if (converted == c){
            printf("Conversion unsuccesful");
        }
        else {
            printf("Converted value: %f",d);
        }
    }
}

Quoting from the man page:

   double strtod(const char *nptr, char **endptr);

If endptr is not NULL, a pointer to the character after the last character used in the conversion is stored in the location referenced by endptr.

If no conversion is performed, zero is returned and the value of nptr is stored in the location referenced by endptr.

Also I would use getline rather than gets to avoid problems in case the input stream is too big.

Upvotes: 1

Related Questions