我正在使用文本文件中的數據填充數組的程序。當我輸出數組時,其內容並不按照我認爲讀入的順序排列。我在考慮問題是在將數據輸入到數組中還是將數組輸出到iostream的for循環中。任何人都能發現我的錯誤嗎我是填寫這個數組錯誤還是輸出錯了?
數據:
(I改變第一數量每行中,以2-31從0和1的區別開來)
輸出:
該代碼:
#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;
}
蕩它,這就是爲什麼我用2個數組,但最初的要求只是想一個數組!我將如何解決這個問題? – darko 2011-03-10 22:30:31
我在你之前關於這個循環的問題中建議的方式:P - http://stackoverflow.com/questions/5239689/reading-data-from-file-into-array - 只使用一個數組,並且不要重置CurRow之後閱讀第一課 – Erik 2011-03-10 22:31:44
更新了建議的修復程序。這沒有任何錯誤檢查,我仍然認爲你的其他Q的循環機制會更好。 – Erik 2011-03-10 22:37:14