到达线路时程序崩溃
Program crashes when reaching a line
程序到达player[aplyr].grid[X][Y] = isSHIP;
时崩溃
const char isSHIP = 'S';
struct PLAYER{
char grid[10][10];
}player[3]; //player 1 and 2. Ignore player 0
int main ()
{
int X;
int Y;
//"PLACE SHIPS" phase of game
//Loop through each player...
for (int aplyr=1; aplyr<3; ++aplyr)
{
//Loop through each ship type to place
for (int thisShip=0; thisShip<15; ++thisShip)
{
//Get input from user
PlaceShips();
//Add the FIRST grid point to the current player's game board
player[aplyr].grid[X][Y] = isSHIP;
}
//Loop back until 15 points have been placed
}
//Loop back through each player
}
void PlaceShips()
{
int x, y, player;
bool goodInput = false;
do {
//get X pick
cout << "Enter X and Y coordinates: ";
cin >> x >> y;
if (x >= 10 && y>=10)
{
goodInput = false;
cin.clear();
cout << "Out of Range!";
break;
}
else
{
goodInput = true;
}
} while (!goodInput);
}
在 main 函数中,您正在创建变量 X 和 Y,而无需初始化它们的值。这意味着它们可以包含任何值,并且很可能超出网格范围。这将导致程序在指定行崩溃。
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- 试图创建流或fopen时程序崩溃
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- DLL Made with CMake 使程序崩溃
- 程序崩溃使用boost::asio
- 调用 free() 有时会导致程序崩溃
- 邮件加密程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- For 循环在尝试读取数组 c++ 时程序崩溃
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- Windows桌面程序保存您的计算机会话 - 基于程序崩溃时的恢复会话
- 无法访问的代码如何导致我的程序崩溃?
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- QCompleter set模型使应用程序崩溃
- 使用唯一指针调用函数会使我的程序崩溃
- 删除SDL_PollEvent时程序崩溃