Reputation: 2458
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)
The output:
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;
}
Upvotes: 3
Views: 440
Reputation: 31435
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.
Upvotes: 0
Reputation: 91260
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] ;
Upvotes: 1
Reputation: 10458
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
}
Upvotes: 0
Reputation: 29450
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.
Upvotes: 1