Error with 2D Array and Void Function

Thanks Chervil! Managed to figure out the issue.

Deleted code for safety measure.
Last edited on
Can anyone help me? This assignment is worth a large percentage of my grade and it's going to be due in the next 2 hours.
Last edited on
You can read from the file like this:

1
2
3
4
5
6
7
8
    int row = 0;
    int a, b, c, d;
    
    while ( (row < 6) && (inFile >> a >> b >> c >> d) )
    {
        cout << a << " " << b << " " << c << " " << d << " " << endl;
        row++;
    }


Here, the variable int row keeps track of how many rows have been read from the file.

The cout statement can be replaced by code to copy the values to the array,
1
2
3
4
        factories [row][0] = a;
        factories [row][1] = b;
        factories [row][2] = c;
        factories [row][3] = d;

or indeed, get rid of a-d completely and just just put the factories array directly in the infile >> statement.

Note: I added an extra test (row < 6) in order to avoid running beyond the actual array, if for example the file had more than six lines of data.

On the other hand, after the loop has completed, row will hold the actual number of rows which were successfully read from the file.

Oh, one more thing, avoid looping on eof(), it often leads to errors, it is not recommended.
Thanks for the info! Managed to properly display the values.
Thanks for the feedback, glad you made some progress in the time available. :)
Deleted code for safety measure.

No. Don't do that. When you do that, you ruin this thread as a resource for others to learn from. It's a selfish abuse of these forums, and of the people who've taken the time to help you.

Please put your code back.
Topic archived. No new replies allowed.