【发布时间】:2018-05-12 11:22:52
【问题描述】:
我正在编写一个简单的 Game Of Life 模拟器。一切都很顺利,除了最后,当 cout 打印结果时,我得到一个中断错误。我不明白为什么,我想寻求您的帮助。
变量
#include <iostream>
using namespace std;
struct cell
{
bool isAlive;
int posX;
int posY;
int numberOfAliveNeighbours;
char group;
};
int cellNumber;
cell *cellTable = new cell[cellNumber];
int numberOfTunrs;
主要:
int main()
{
int x;
int y;
int cellCounter = 0;
cin >> x >> y;
cellNumber = x*y;
cin >> numberOfTunrs;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
char cellAliveChar;
cin >> cellAliveChar;
if (cellAliveChar == '#')
{
cellTable[cellCounter].isAlive = true;
}
else if (cellAliveChar == '.')
{
cellTable[cellCounter].isAlive = false;
}
cellTable[cellCounter].numberOfAliveNeighbours = 0;
cellTable[cellCounter].group = '#';
cellTable[cellCounter].posX = j;
cellTable[cellCounter].posY = i;
cellCounter++;
}
}
doTurns(x, y);
int result;
result = countGroups();
**cout << result << endl;**
//here is breakpoint
cin >> x;
}
countGroups(如果相关,idk):
int countGroups()
{
int max = 0;
int current;
int i = 0;
char checkingGroup = 'A';
do
{
current = 0;
for (int j = 0; j < cellNumber; j++)
{
if (cellTable[j].group == checkingGroup + i)
{
current++;
}
}
i++;
if (current > max)
{
max = current;
}
} while (current != 0);
return max;
}
断点截图:
【问题讨论】:
-
**cout << result << endl;**为什么?,如果你已经做了 using namespace std 或std::cout << result << std::endl;,你的意思是cout << result << endl; -
} while (current = 0);应该是} while (0 == current);或一些适当的条件。也不清楚cellTable是什么以及doTurns内部发生了什么很可能是某种缓冲区溢出破坏程序状态。 -
@VTT 您好,我添加了全局变量并对其进行了编辑。断点仍然出现:(
-
@xanadev 是的,我只是想标记断点出现在代码中的位置 :) 我编辑了我的帖子并添加了变量和包含,我正在使用命名空间 std,您可能想要检查
标签: c++ visual-studio