任何人都可以修復此示例代碼,該代碼將打印2D數組中的文件。這裏是代碼和輸出。使用二維數組打開流
while (!file.eof())
{
int counter =0;
file>>n;
cout<< setw(4)<< n << " ";
if (counter == 5)
{
cout << endl;
counter = 0;
counter ++;
}
}
}
輸出不是表格形式。
的輸出是:
指數大小重量(磅/英尺)直徑(英寸)0 2 0.167 0.250 1 3 0.376 0.375 2 4 0.668 0.500 3 5 1.043 0.625 4 7 1.502 0 6 9 2.670 1.000 7 12 3.400 1.128 8 14 4.303 1.270 1.270
按任意鍵繼續。 。 。
你設置'計數器= 0'在循環的每次迭代的頂部,所以它永遠不會達到5 ...移動你的'計數器'變量的定義在循環之前...而且,如果它已經是5,你只會增加它,所以還有另一個原因,它永遠不會達到5 ... – twalberg