Is there a way to use scanf (without using any other I/O function) to check whether the user input exists only of a single integer with nothing following it?
For example:
int g;
while(scanf("%d", &g)!=1){
printf("\nincorrect input, try again");
}
This works fine for input like "/" or "-" but when you input "54.32" then it will read until the ".", the read count of scanf will be 1 and 54 will be stored in g. Is there a way to check if the input consists solely of a single integer followed by nothing but a newline character?
I would like to know if there exists a solution without using fgets or any other IO function?
"%d%c"
and then if the return is 2 and the character isn't'\n'
-- extraneous characters exist. (but you really should do all user-input withfgets()
and if needed conversions withsscanf()
) – David C. Rankin