0
所以我的代碼的前提是從.txt讀取二維數組。陣列是一個遊戲板,但前兩行是決定棋盤大小的線。當我讀到它後,我希望它找到字符「U」的位置,然後顯示數組,但只顯示U和它周圍的內容。問題是我不能讓陣列打印正確的大小和顯示U的代碼也不工作。閱讀二維數組並顯示它(C++)
ifstream inputFile;
int boardSizeRow;
int boardSizeCol;
inputFile.open("C:\\Users\\Michael\\Desktop\\fileboard2.txt");
inputFile >> boardSizeRow;
inputFile >> boardSizeCol;
inputFile.get();
char gameBoard[20][20];
for (int row = 0; row < boardSizeRow; row++)
{
for (int col = 0; col < boardSizeCol; col++)
{
gameBoard[row][col] = inputFile.get();
}
}
for (int row = 0; row < boardSizeRow; row++) //////////////TO TEST PRINT
{
for (int col = 0; col < boardSizeCol; col++)
{
cout << gameBoard[row][col];
}
}
cout << endl;
cout << endl;
const int ROWS = 20;
const int COLS = 20;
bool toPrint[ROWS][COLS] = {false};
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (gameBoard[i][j] == 'U')
{
//set parameters around:
toPrint[i][j] = true;
toPrint[i][j-1] = true; //West
toPrint[i][j+1] = true; //East
toPrint[i-1][j] = true; //North
toPrint[i+1][j] = true; //South
}
}
}
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (toPrint[i][j])
{
cout << gameBoard[i][j] ;
}
else
{
cout <<"0";
}
}
cout <<endl;
}
cout << endl;
return 0;
。 txt文件::
20
20
WWWWWWWWWWWWWWWWWWWW
W GO W W
W WW w S W
W H W GW w W
WPW WW G W
WK W W
W W W W w w W
WK WU W
SW w w W
W W
w W G W
G W w W
D wwwww W
K w D W
w w W w w W
ww w WWWWWWW
G w W
ww w S w W
WWW G W
WWWWWWWWWWWWWWWWWWWW
你什麼輸出?你可以編輯問題並將其添加到它嗎? –