I need to make a function that gets input from the user and makes sure that it is a whole number and does not contain any character.
I wrote this code which works perfectly for integers and for single characters. But it terminates if I enter dfd
i.e. multiple character input. Below is my code compiled with gcc on Linux:
#include <ctype.h>
int getint()
{
int input = 0;
int a;
int b, i = 0;
while((a = getchar()) != '\n')
{
if (a<'0'||a>'9')
{
printf("\nError in input!Please try entering a whole number again:");
input=0;
fflush(stdin);
return getint();
}
b = a - '0';
input = ((input*10) + b);
i++;
}
return input;
}