Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am i filling this array wrong or outputting it wrong?

Tags:

c++

arrays

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;
}
like image 666
darko Avatar asked Feb 18 '26 11:02

darko


2 Answers

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] ;
like image 144
Erik Avatar answered Feb 21 '26 01:02

Erik


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.

like image 29
zdan Avatar answered Feb 21 '26 01:02

zdan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!