I
interpim
ok the function is supposed to accept only non-negative integers... but it still accepts letters input through the function. Such as '23e' still passes through. I tried to use isalpha in the or statement to cause an error, but it causes an error for everything input. How do I make an error occur when anything other than numbers are put in?
#include <stdio.h>
#define MAXLINE 20
int main(void)
{
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);
}
#include <stdio.h>
#define MAXLINE 20
int main(void)
{
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);
}