6

I am trying to read specific data from a file into two 2D arrays. The first line of data defines the size of each array so when I fill the first Array i need to skip that line. After skipping the first line, the first array fills with data from the file until the 7th line in the file. The second array is filled with the rest of the data from the file.

Here's a labeled image of my data file: enter image description here

and here's my (flawed) code so far:

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    ifstream inFile;
    int FC_Row, FC_Col, EconRow, EconCol, seat;

    inFile.open("Airplane.txt");

    inFile >> FC_Row >> FC_Col >> EconRow >> EconCol;

    int firstClass[FC_Row][FC_Col];
    int economyClass[EconRow][EconCol];

    // thanks junjanes
    for (int a = 0; a < FC_Row; a++)
        for (int b = 0; b < FC_Col; b++)
            inFile >> firstClass[a][b] ;

    for (int c = 0; c < EconRow; c++)
        for (int d = 0; d < EconCol; d++)
            inFile >> economyClass[c][d] ;

    system("PAUSE");
    return EXIT_SUCCESS;
}

Thanks for the input everyone.

3
  • 1
    int firstClass[FC_Row][FC_Col]; is a VLA which is C99, not c++. Some c++ compilers support it, it's bad for portability though. Commented Mar 8, 2011 at 23:40
  • +1 for your clearly illustrated diagram. MSPaint gets a +1 from me :-) Commented Mar 8, 2011 at 23:44
  • +1 for providing a sample of your program. Commented Mar 8, 2011 at 23:46

3 Answers 3

3

Your while loops iterate until the end of file, you don't need them.

while (inFile >> seat) // This reads until the end of the plane.

Use instead (without the while):

for (int a = 0; a < FC_Row; a++)         // Read this amount of rows.
     for (int b = 0; b < FC_Col; b++)    // Read this amount of columns.
         inFile >> firstClass[a][b] ;    // Reading the next seat here.

Apply the same for economic seats.


Also you might want change arrays into vectors, since variable size arrays are hell.

vector<vector<int> > firstClass(FC_Row, vector<int>(FC_Col)) ;
vector<vector<int> > economyClass(EconRow, vector<int>(EconCol)) ;

You need to #include <vector> to use vectors, their access is identical to arrays.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to change the order of the for loops and reading from the file:

for (rows = 0; rows < total_rows; ++ rows)
{
  for (col = 0; columns < total_columns; ++cols)
  {
    input_file >> Economy_Seats[row][column];
  }
}

I'll leave checking for EOF and handling of invalid input to the reader.

Comments

1

You're reading into seat once then filling the array with this value. Then you're reading into seat again, and filling the entire array with this new value.

Try this:

int CurRow = 0;
int CurCol = 0;
while ( (inFile >> seat) && (CurRow < FC_Row)) {
  firstClass[CurRow][CurCol] = seat;
  ++CurCol;
  if (CurCol == FC_Col) {
    ++CurRow;
    CurCol = 0;
  }
}
if (CurRow != FC_Row) {
  // Didn't finish reading, inFile >> seat must have failed.
}

Your second loop should use economyClass not firstClass

The reason for switching the loop around like this is error handling, which is simplified when the loop exits upon error. Alternatively you could keep the for loops, use infile >> seat in the inner loop, but you'd then have to break out of two loops if reading failed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.