0

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.

5
  • 2
    Are you trying to read the variables twice? If not, remove the scanf after the first printf call. Also, the %c isn't really needed. Commented Jul 3, 2014 at 14:40
  • 2
    If you want to read a positive (i.e. unsigned) integer, why are you using the "%i" format code (which is signed)? Shouldn't you be using "%u" instead? Commented Jul 3, 2014 at 14:41
  • @Hasturkun Even after removing it does not work. Commented Jul 3, 2014 at 14:41
  • @JoachimPileborg Anyhow even after changing it to %u the program does nothing after having two inputs and says invalid output after the third input. Commented Jul 3, 2014 at 14:49
  • @Abhirup ,Now(After EDIT 3) your program dosen't need the variable check.so remove it.Also,merge the two if(...) into one if(...) . You got an extra comma in your scanf.remove it. Commented Jul 3, 2014 at 15:22

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll note that if he really wants to read the newline, the format string should be %i %i%c, as otherwise it scanf will consume the newline (and any other whitespace as well)
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'){ ....

4 Comments

I tried your code. But even then I keep on pressing return after entering two integers and nothing happens. But if enter a character after the two integers it shoes 'invalid output' @RickyMutschlechner
Hi @AbhirupMondal, I've edited my answer. Please take a look.
@RickyMutschlechner using %u does not verify that the integers are positive it just tells scanf to try and interpret the users input as such, please see this example it interprets the 2s compliment -4294967280 as (+)16...
Using your numArgs = 2 version it somewhat works. But a need to input a character other than newline to find out the H.C.F. Else I go on pressing Enter indefinitely .@RickyMutschlechner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.