Program ignores the first scan of the loop

Asked

Viewed 28 times

0

Basically I have this program

int main(){
    coordenada lista1_coordenadas[3];
    coordenada lista2_coordenadas[3];

    for (int i=0;i<3;i++){
        printf("Leia a coordenada %d da primeira lista:\n", i+1);
        printf("x: ");
        scanf("%d\n", &lista1_coordenadas[i].x);
        printf("y: ");
        scanf("%d\n", &lista1_coordenadas[i].y);
    }
    for (int i=0;i<3;i++){
        printf("Leia a coordenada %d da segunda lista:\n", i+1);
        printf("x: ");
        scanf("%d\n", &lista2_coordenadas[i].x);
        printf("y: ");
        scanf("%d\n", &lista2_coordenadas[i].y);
    }
    return 0;

But when I scan the x of the first coordinate of the list 1, it ignores the first scan and asks to input the value again, and I don’t know how to fix it.

Here’s the struct I used

typedef struct coordenada{
    int x, y;
}coordenada;

1 answer

1

Strip the n of the scanfs. You only need to put it if the later reading is of characters or strings.

     scanf("%d", &lista1_coordenadas[i].x);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.