我正在尝试将文本文件读入2D数组。行和列列在文本文件的前两行中,所以我只是:
inFile >> rows;
inFile >> cols;
并且工作正常。
但是,每当我初始化阵列并打印它时......它都不能正确显示。为什么?迷宫中应该有空格,但所有东西都充满了#,如此:
# # # # # # # # #
# # # # # # # # #
# # # # # s # # #
e # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # #
void setArray(int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
grid[i] = new string[cols];
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
inFile >> grid[i][j];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << grid[i][j] << " ";
}
cout << endl;
}
}