Validate input in C

2019-08-30 06:24发布

问题:

I want to write a program so to read two POSITIVE INTEGER as input and reject if the user inputs anything other than two positive integers.I tried to use the following code but it does not work.

EDIT 1: Removed the first scanf. EDIT 2: Added code to check for negative values.

The code which does not work:

#include <stdio.h>
#include <stdlib.h>

int main () {

unsigned int no1,no2,temp;
char check;
printf("Enter two positive integers.\n");
scanf("%i %i %c", &no1, &no2 ,&check);

if(scanf("%i %i %c", &no1, &no2,&check) != 3 || check != '\n'){
    printf("Invalid input!!.\n");
    exit(EXIT_FAILURE);

}
else if (no1 <= 0 || no2 <= 0) {

        printf("Invalid input!!.\n");
        exit(EXIT_FAILURE);

     }



int copy1,copy2;

copy1 = no1;
copy2 = no2;

while(no2 != 0) {

    temp = no1 % no2 ;
    no1 = no2;
    no2 = temp ;
}

printf("The H.C.F. of %i and %i is %i. \n",copy1,copy2,no1);

return 0;

}

The working code:

#include <stdio.h>
#include <stdlib.h>

int main () {

int no1,no2,temp;

printf("Enter two positive integers.\n");
int numArgs = scanf("%i%i", &no1, &no2 );


if( numArgs != 2|| no1 <= 0 || no2 <= 0 ){
    printf("Invalid input!!.\n");
    exit(EXIT_FAILURE);
}




int copy1,copy2;

copy1 = no1;
copy2 = no2;

while(no2 != 0) {

    temp = no1 % no2 ;
    no1 = no2;
    no2 = temp ;
}

printf("The H.C.F. of %i and %i is %i. \n",copy1,copy2,no1);

return 0;

}

It goes on till I input 5 integers or 2 characters consecutively other than \n. It never computes the H.C.F. However it works if I remove the "if" block.

EDIT 3: Now i do not want to read the newline.

The second if block to check for negative values is also not working.

回答1:

You have two problems in the code as shown in the question (before the edit):

  1. You call scanf twice for the same variables, forcing the user to input the same data twice.

  2. The format used will not read a newline, so the expression check != '\n' will always be true.

For number 1, just remove the first scanf call. For number 2, the user must press the Enter key anyway to end the input, so no need to check for that. If you really want to be sure, then use e.g. fgets to read a line with both number in it, and use sscanf to parse the values.



回答2:

 scanf("%i %i %c", &no1, &no2 ,&check);

    if(scanf("%i %i %c", &no1, &no2 ,&check ) != 3 || check != '\n' )

The problem lies here. You're calling scanf() twice, meaning it's checking for input twice. If you want to check the number of inputs, simply save the return value of scanf the first time you get it from the user; Also, unsigned ints should be "scanned" with %u, not %i. Read more about scanf() here.

Also, I believe that scanf does not actually take into account the newline character. However, the user must push enter to submit the input to you anyways, so you don't really have to check for it.

    int numArgs = scanf("%u %u", &no1, &no2);

if(numArgs != 2){ ....

However, if you DID want to check for the newline character for some reason, try this:

int numArgs = scanf("%u %u%c", &no1, &no2, &check);
if(numArgs != 3 || check != '\n'){ ....


标签: c scanf