3

I am working on a program that fills an array with data from a text file. When I output the array its contents are not in the order I thought I read them in. I'm thinking the problem is either in one of the for loops that inputs data into the array or outputs the array to the iostream. Can anyone spot my mistake?

The data:

(I changed the first number in each row to 2-31 to differentiate it from the 0's and 1's) enter image description here

The output:

enter image description here

The code:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

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

    inFile.open("Airplane.txt");

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

    int airplane[100][6];

    int CurRow = 0;
    int CurCol = 0;

    while ( (inFile >> seat) && (CurRow < FC_Row)) 
    {
     airplane[CurRow][CurCol] = seat;
     ++CurCol;
      if (CurCol == FC_Col)
       {
       ++CurRow;
       CurCol = 0;
       }
    }


while ( (inFile >> seat) && (CurRow < EconRow)) 
{
 airplane[CurRow][CurCol] = seat;
 ++CurCol;
  if (CurCol == EconCol)
    {
     ++CurRow;
     CurCol = 0;
    }
 }

    cout << setw(11)<< "A" << setw(6) << "B"
    << setw(6) << "C" << setw(6) << "D"
    << setw(6) << "E" << setw(6) << "F" << endl;
    cout << " " << endl;

    cout << setw(21) << "First Class" << endl;
    for (a = 0; a < FC_Row; a++)
    {
        cout << "Row " << setw(2) << a + 1;
        for (b = 0; b < FC_Col; b++)
        cout << setw(5) << airplane[a][b] << " ";

        cout << endl;
    }

    cout << setw(23) << "Economy Class" << endl;
    for (a = 6; a < EconRow; a++)
    {
        cout <<"Row " << setw(2)<< a + 1;
        for (b = 0; b < EconCol; b++)
        cout << setw(5) << airplane[a][b] << " ";

        cout << endl;
    }


    system("PAUSE");
    return EXIT_SUCCESS;
}
darko
  • 2,438
  • 8
  • 42
  • 54

4 Answers4

1

You're filling it wrong.

for (a = 0; a < 100; a++)    
    for (b = 0; b < 6; b++)

The above loop doesn't match up very well with the first lines of your file, where you don't have 6 elements per row.

In the first inner loop, you will read 2, 1, 1, 1, 3, 0 into airplane[0].

EDIT: The fix.

for (a = 0; a < FC_Row; a++)    
    for (b = 0; b < FC_Col; b++)
        inFile >> airplane[a][b] ;

for (a = 0; a < EconRow; a++)    
    for (b = 0; b < EconCol; b++)
        inFile >> airplane[a+FC_Row][b] ;
Erik
  • 88,732
  • 13
  • 198
  • 189
  • Dang it, Thats why I used 2 arrays initially but the requirements only want one array! How would I get around this? – darko Mar 10 '11 at 22:30
  • The way I suggested in your previous question about this loop :P - http://stackoverflow.com/questions/5239689/reading-data-from-file-into-array - Just use one array and don't reset CurRow after reading first class – Erik Mar 10 '11 at 22:31
  • Updated with a suggested fix. This has no error checking whatsoever, and I still think the loop mechanism from your other Q would be better. – Erik Mar 10 '11 at 22:37
  • Ah yeaaa, I edited the code, I am not sure how I can make the second while loop fill correctly though. (unless im outputting it wrong.) – darko Mar 10 '11 at 22:55
1

Your code that fills the array:

   for (a = 0; a < 100; a++)    
        for (b = 0; b < 6; b++)
            inFile >> airplane[a][b] ;

assumes that there are 6 columns in every row, there aren't, there are only 4 rows in the first 6 rows.

zdan
  • 28,667
  • 7
  • 60
  • 71
0

so you're filling in a 100x6 array, but first few rows of data only have 4 columns of data.

A better way is something like this:

 for (a = 0; a < 100; a++)    
        for (b = 0; b < 6; b++)
        {
          char c;
          inFile>>c;
          if (c is new line){
            break;
          }

          //fill in the 2d array
        }
Alvin
  • 10,308
  • 8
  • 37
  • 49
0

The correct approach here is to read in a line at a time with std::getline. Then parse each line, similar to the way you are, albeit you might want to use vectors rather than 2-dimensional arrays.

If you had a vector of vectors, you would find that the inner vectors do not need to all have the same size, and indeed they should not in your case.

As it is, what I do not get is that you are reading in the values for EconRow and EconCol yet hard-coding your array size.

With vector you would be able to flexibly set this to the value you had read in.

CashCow
  • 30,981
  • 5
  • 61
  • 92