C++ 我是填充这个数组错误还是输出错误?
我正在编写一个程序,用文本文件中的数据填充数组。当我输出数组时,它的内容与我认为的读取顺序不一致。我认为问题要么出在将数据输入数组的for循环中,要么出在将数组输出到iostream的for循环中。有人能看出我的错误吗 数据:C++ 我是填充这个数组错误还是输出错误?,c++,arrays,C++,Arrays,我正在编写一个程序,用文本文件中的数据填充数组。当我输出数组时,它的内容与我认为的读取顺序不一致。我认为问题要么出在将数据输入数组的for循环中,要么出在将数组输出到iostream的for循环中。有人能看出我的错误吗 数据: #include <cstdlib> #include <iostream> #include <fstream> #include <iomanip> using namespace std; int main() {
#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-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;
}
代码:
#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
河流充填;
int FC_Row,FC_Col,EconRow,EconCol,座椅,a,b;
infle.open(“framework.txt”);
填充>>FC_Row>>FC_Col>>EconRow>>EconCol;
国际飞机[100][6];
int CurRow=0;
int CurCol=0;
而((填充>>座椅)和&(当前>座位)和&(当前 你填错了
for (a = 0; a < 100; a++)
for (b = 0; b < 6; b++)
填充数组的代码:
for (a = 0; a < 100; a++)
for (b = 0; b < 6; b++)
inFile >> airplane[a][b] ;
(a=0;a<100;a++)的
对于(b=0;b<6;b++)
填充>>飞机[a][b];
假设每行中有6列,没有,前6行中只有4行。因此您正在填充100x6数组,但前几行数据只有4列数据
更好的方法是这样的:
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
}
(a=0;a<100;a++)的
对于(b=0;b<6;b++)
{
字符c;
填充>>c;
如果(c是新行){
打破
}
//填充二维数组
}
这里正确的方法是使用std::getline一次读入一行。然后解析每一行,与您的方式类似,尽管您可能希望使用向量而不是二维数组
如果你有一个向量的向量,你会发现内部向量不需要都有相同的大小,事实上在你的例子中它们不应该都有相同的大小
事实上,我没有得到的是,您正在读取EconRow和EconCol的值,但对数组大小进行了硬编码
使用vector,您可以灵活地将其设置为您读入的值。Dang it,这就是为什么我最初使用了两个数组,但需求只需要一个数组!我该如何解决这个问题?正如我在上一个问题中关于这个循环的建议:p--只使用一个数组,在阅读第一个更新的数组后不要重置CurRow这是一个建议的修复。这没有任何错误检查,我仍然认为你的另一个Q的循环机制会更好。啊,是啊,我编辑了代码,我不知道如何才能使第二个while循环正确填充。(除非我输出错误。)