DigitalHero
DigitalHero

Reputation: 13

I am trying to take a single input from user but somehow the computer asks for two inputs?

 void coordinateInput(int in){
     int * input = (int *)malloc(in*(sizeof(int)));
     for (int i = 0; i < in; i++){
         printf("Please enter the x coordinate for control point #%d: ", i); 
         scanf("%d\n",&input[i]);
         printf("Please enter the y coordinate for control point #%d: ", i); 
         scanf("%d\n",&input[i+1]);
     }
 }

In output you can see after line 0 it asks for another input: Screenshot of output

I want the to get only one input but I end up having the input twice for some reason. Its only true for first case only.

Upvotes: 1

Views: 39

Answers (1)

luisreys
luisreys

Reputation: 46

I fixed it removing '\n' from the scanf statement and adding it in the printf.

int * input = (int *)malloc(in*(sizeof(int)));
 for (int i = 0; i < in; i++){
 printf("Please enter the x coordinate for control point #%d: ", i); 
 scanf("%d",&input[i]);

 printf("\nPlease enter the y coordinate for control point #%d: ", i); 
 scanf("%d",&input[i+1]);
 //[In output you can see after line 0 it asks for another input][1]}
 printf("%d - %d\n", input[i], input[i+1]);
}

The code should be something like that.

Upvotes: 1

Related Questions