-
sscanf problems
I have made a program to accept integers only however if i put a 25e in it it still works how do i fix it?
Code:
char line[MAXLINE];
int error, n;
do
{
printf("Input a positive integer: ");
fgets(line, MAXLINE, stdin);
error = sscanf(line, "%d", &n) != 1 || n <= 0;
if(error)
printf("\nERROR: Do it again. \n");
}while (error);
-
Use strtol(). It will show you if any extra non-digit characters were in the buffer.
-
i have to use sscanf and i am not familiar with this
-
Then I believe you have to manually check variable line yourself and see if it contains any non-digit chars.
-
-
Or that. Nice solution. :)