Alzer Casiño
Alzer Casiño

Reputation: 73

Why does the compiler skip my second scanf loop?

This is a program for adding matrices but it seems that the compiler skips the second scanf in the second nested for loop.

void input_add_matrices() {
    printf("Input number of rows for matrices: "); scanf("%d", &nrow_matrix1); getchar();
    printf("Input number of columns for matrices: "); scanf("%d", &ncolumn_matrix1); getchar();
    printf("\nInput elements for matrix 1:\n");
    for(i=0; i<nrow_matrix1; i++){
        for(j=0; j<ncolumn_matrix1; j++){
            scanf("%d", &element_matrix1[i][j]); getchar();
        }
    }
    nrow_matrix2=nrow_matrix1;
    ncolumn_matrix2=ncolumn_matrix2;
    printf("\nInput elements for matrix 2:\n");
    for(i=0; i<nrow_matrix2; i++){
        for(j=0; j<ncolumn_matrix2; j++){
            scanf("%d", &element_matrix2[i][j]); getchar();
        }
    }
}

Is this a compiler error or something else? Please help.

Output is:

Input number of rows for matrices: 2
Input number of columns for matrices: 2

Input elements for matrix 1:
1
2
1
2

Input elements for matrix 2:

Matrix 2 is blank.

Upvotes: 0

Views: 104

Answers (1)

Stephan Lechner
Stephan Lechner

Reputation: 35154

I think it's the code line ncolumn_matrix2=ncolumn_matrix2; which leads to ncolumn_matrix2 to be 0, such that the loop is not entered

Upvotes: 5

Related Questions