-
sscanf help
I need help mofiying this code. As is right now, it will catch some typing errors. But if I type 54y it will not catch it. How can I modify this code so it will catch those type of errors?
Code:
#define MAXLINE 100
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("\nDo it again!\n");
} while(error)
-
Code:
#include <stdio.h>
#include <string.h>
#define MAXLINE 100
int main(void)
{
char line[MAXLINE];
int error, n, x;
do{
printf("Input a positive integer: ");
fgets(line, MAXLINE, stdin);
error = sscanf(line, "%d%n", &n, &x) != 1
|| x < strlen(line) - 1
|| n <= 0;
if(error)
printf("\nDo it again!\n");
} while(error);
return 0;
}
-