-2

I'm try to make a program that import a grid from a data file and does something with it, but I'm running into problems when trying to import the grid into a multidimensional array.

I'm getting an "Unhandled Exception" error with the if-else statement inside my while loop. When I comment out this part of my code it runs perfectly.

int main( void ) {
    FILE* grid = fopen( FILE_NAME, "r" );
    int row = 0;
    int column = 0;
    int intGrid[21][21];

    // Null check omitted for space.

    while( fscanf( grid, "%d ", &intGrid[row][column] ) == 1 ) { // Loads the grid into an array
        if( intGrid[row][column] != -1 )
            column++;
        else
            column = 0;
            row++;
    }

Can anyone please spot the problem in my code.

Dominic
  • 1
  • 1

1 Answers1

0

Missing brackets:

else { 
    column = 0;
    row++;
}

Withot brackets the row is always incremented and would quickly run out of array dimensions.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43