我无法找到我的错误是我的代码。我试图创建一个"董事会"用X和Y作为坐标绘制一些东西。问题是我想要绘制的字符不止一次出现。这是代码:
class Board
{
public:
void fillBoard()
{
for (int i = 0; i < 22; i++)
{
for (int k = 0; k < 78; k++)
{
board[i][k] = ' ';
}
}
}
void drawBoard()
{
for (int i = 0; i < 22; i++)
{
for (int k = 0; k < 78; k++)
{
cout<<board[i][k];
}
cout<<"\n";
}
}
void addView(int width, int height, char img, int tallness = 0)
{
/// board[height][width] = img;
for (int i = 0; i <= tallness; i++, height++)
{
board[height][width] = img;
}
}
private:
char board[78][22];
};
int main()
{
Board board;
board.fillBoard();
///board.addView(x, y, 'O');
board.addView(0, 10, '|', 5);
board.drawBoard();
return 0;
}
如您所见,划桨出现了4次。我希望它在给定的坐标处只出现一次。我的错是什么?
答案 0 :(得分:3)
Steve Cox已经指出你正在向后索引数组。这样:
for (int i = 0; i < 22; i++)
{
for (int k = 0; k < 78; k++)
{
board[i][k] = ' ';
}
}
可以在数组board[22][78]
上运行,但是您声明board[78][22]