hi everyone i'm programming validation of a number that only accepts positive integers the code below is for only integers
But when i try to add it the validation of positivesCode:
- #include<ctype.h>
- #include<stdio.h>
- int main(void)
- {
- char buf[BUFSIZ];
- int value, end;
- printf("Enter an integer: ");
- fflush(stdout);
- while(fgets(buf,sizeof buf, stdin)== NULL ||
- sscanf(buf,"%d%n",&value,&end)!=1||
- !isspace(buf[end]))
- {
- printf("Invalid integer. Please try again: ");
- fflush(stdout);
- }
- printf("You entered %d\n", value);
- return0;
- }
And when i compile ignores the validation of only positives. Help please.Code:
- #include<ctype.h>
- #include<stdio.h>
- int main(void)
- {
- char buf[BUFSIZ];
- int value, end;
- printf("Enter an integer: ");
- fflush(stdout);
- while(value<0) { printf("Only positives numbers. Please try again: "); fflush(stdout); }
- while(fgets(buf,sizeof buf, stdin)== NULL ||
- sscanf(buf,"%d%n",&value,&end)!=1||
- !isspace(buf[end]))
- {
- printf("Invalid integer. Please try again: ");
- fflush(stdout);
- }
- printf("You entered %d\n", value);
- return0;
- }