问题显示数组。一切都放在单独的行中,而不是放在单独的列中吗?

时间:2019-05-05 18:54:13

标签: c++

我正在研究基于文本的扫雷程序,作为一项家庭作业。我在显示数组时遇到问题,但没有弄清楚我所缺少的内容。基本上,我试图使程序在行和列中显示“ 5”,以确保正确编写了该程序。但是,它在一长列中显示一串100 5。我相信它将行数与列数相乘,只能假定是10乘10。是什么导致了此错误?

感谢您的帮助。

#include <iostream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
using namespace std;

const int MAX_ROWS = 10;
const int MAX_COLUMNS = 10;
const int EMPTY_SQUARE_DIGIT = 0;
const char EMPTY_SQUARE_SYMBOL = '_';
const int BOMB_DIGIT = -1;
const char BOMB_SYMBOL = '#';

void fillTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS]);
void displayTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS]);

int main(void)
{
int gameBoard[MAX_ROWS][MAX_COLUMNS];

srand(time(NULL));

fillTheGameBoard(gameBoard);
displayTheGameBoard(gameBoard);




system("pause");

return 0;
}


void fillTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS])
{
    for (int row = 0; row < MAX_ROWS; row++)

        for (int column = 0; column < MAX_COLUMNS; column++)
        {
            board[row][column] = 5;
        }
} 


void displayTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS])
{
    for (int row = 0; row < MAX_ROWS; row++)

        for (int column = 0; column < MAX_COLUMNS; column++)
        {
            cout << board[row][column] << " " << endl;
        }
}

1 个答案:

答案 0 :(得分:0)

为每个单元格打印换行符。

cout << board[row][column] << " " << endl;

这说明了“所有内容都放置在单独的行中”位。您可能知道现在要解决的问题。

for (int row = 0; row < MAX_ROWS; row++)
{
    for (int column = 0; column < MAX_COLUMNS; column++)
        cout << board[row][column] << " ";
    cout << endl;
}