gpt4 book ai didi

c++ - 使用2d数组时,为什么我的程序中出现段错误(核心转储)错误?

转载 作者:行者123 更新时间:2023-12-02 09:50:23 27 4
gpt4 key购买 nike

我正在尝试用c++(https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life)创建Conway的生活游戏,我不知道为什么会引发此错误。这是我的代码:

#include <iostream>

using namespace std;
void Life() {
//initializing all the variables needed
int creatures;
int row;
int column;
int neighbors = 0;
int days = 0;
char cont = 'c';
//creates Board
string Board[20][20];
string NewBoard[20][20];
//stores X for dead into all elements
for (int r = 0; r <= 19; r++) {
for (int c = 0; c <= 19; c++) {
Board[r][c] = "X ";
}
}
//asks for and stores the users coords
cout<<"Enter the amount of creatures desired: ";
cin>>creatures;
for (int i = 0; i <= creatures - 1; i++) {
cout<<"\nEnter the row of the next creature: ";
cin>>row;
cout<<"\nEnter the column of the next creature: ";
cin>>column;
Board[row - 1][column - 1] = "O ";
}
for (int r = 0; r <= 19; r++) {
for (int c = 0; c <= 19; c++) {
NewBoard[r][c] = Board[r][c];
}
}

while (cont == 'c') {
for (int r = 0; r <= 19; r++) {
for (int c = 0; c <= 19; c++) {
Board[r][c] = NewBoard[r][c];
}
}
cout<<"\n\n\n\nDay "<<days<<"\n";
//prints current board
for (int r = 0; r <= 19; r++) {
cout<<"\n";
for (int c = 0; c <= 19; c++) {
cout<<Board[r][c];
}
}
cout<<"\n\n";
//detects for alive creatures then checks them for neighbors
for (int r = 0; r <= 19; r++) {

for (int c = 0; c <= 19; c++) {
//detects for life
if (Board[r][c] == "O ") {
//detects for neighbors
neighbors = 0;
if (Board[r + 1][c + 1] == "O ") {
neighbors++;
}
if (Board[r + 1][c] == "O ") {
neighbors++;
}
if (Board[r + 1][c - 1] == "O ") {
neighbors++;
}
if (Board[r][c + 1] == "O ") {
neighbors++;
}
if (Board[r][c - 1] == "O ") {
neighbors++;
}
if (Board[r - 1][c + 1] == "O ") {
neighbors++;
}
if (Board[r - 1][c] == "O ") {
neighbors++;
}
if (Board[r - 1][c - 1] == "O ") {
neighbors++;
}
//kills the creature if it has less than 2 or more than 3 living neighbors
if (neighbors < 2) {
NewBoard[r][c] = "X ";
}
}
//if creature is dead, checks to see how many live neighbors
else {
neighbors = 0;
if (Board[r + 1][c + 1] == "O ") {
neighbors++;
}
if (Board[r + 1][c] == "O ") {
neighbors++;
}
if (Board[r + 1][c - 1] == "O ") {
neighbors++;
}
if (Board[r][c + 1] == "O ") {
neighbors++;
}
if (Board[r][c - 1] == "O ") {
neighbors++;
}
if (Board[r - 1][c + 1] == "O ") {
neighbors++;
}
if (Board[r - 1][c] == "O ") {
neighbors++;
}
if (Board[r - 1][c - 1] == "O ") {
neighbors++;
}
//"resurrects" creature if it has 3 neighbors
if (neighbors == 3) {
NewBoard[r][c] = "O ";
}
}
}
}
//asks if user wants to continue
cout<<"\n\nPress c to continue, q to quit: ";
cin>>cont;
days++;
}
}

int main()
{
Life();
return 0;
}

抱歉,如果真的很难阅读(我仍在学习)。我已经得出结论,问题来自while循环的其他部分。为什么是这样?

最佳答案

    for (int r = 0; r <= 19; r++) {
for (int c = 0; c <= 19; c++) {

在此循环中,您将同时使用 rc覆盖整个0-19。
                if (Board[r + 1][c + 1] == "O ") {
neighbors++;
}

rc为19时,此处将尝试访问 Board[20][something]Board[something][20](当然不存在)。未定义的行为。崩溃
                if (Board[r - 1][c - 1] == "O ") {
neighbors++;
}

同样,当 rc为0时,这将尝试访问负数组索引。未定义的行为。崩溃

上面仅是几乎所有 if语句都遭受的常见缺陷的示例:缺少适当的边界检查。

关于c++ - 使用2d数组时,为什么我的程序中出现段错误(核心转储)错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60161626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com