I created a function that checks if a given string is a number or not.
The problem is that I take the input with fgets
but ,when called, it doesn't stop to take input from user!
I tried to fix adding fflush(stdin)
and fflush(stdout)
to fix because I read some stuffs online but isn't working :S
getInput
calls the fgets
that stores the string taken from user input then isInt
checks if it's an int or not.
The problem is that the program stucks on the first fgets
.
int isInt (char* string)
{
int k=0;
while( string[k] != '\0')
{
if ( isdigit(string[k])==0) //it's k++ here, I copied bad
return 1;
}
return 0;
}
void getInput(char* string)
{
printf("Insert a number : \n");
while (1)
{
fflush(stdout); //Tried to fix
fflush(stdin); //same as above
fgets(string,sizeof(string),stdin); //stucks here
if(isInt(string)==0 )
{
printf("Ok it's a number!\n");
break;
}
else printf("Insert a valid number!\n");;
}
}