给数组赋值时崩溃
Crash when assigning values to array
这一行代码导致我的程序崩溃。原因是什么?
square [b] = 'T';
其中'square'是一个有64个元素的一维字符数组,b是一个整数。当我给出一个常数而不是b时,例如
,它不会崩溃square [5] = 'T';
编辑:@ smileeaz引起问题的部分…
void traps()
{
int a,b,r;
cout<<"Deciding placement of traps";
for (a = 1; a <= 8; a++)
{
r = clock();
cout<<"nPress any keyn";
getch();
cout<<"nPress any keyn";
getch();
r = r - clock();
b = r % 64;
if (square [b] == '.')
{
square[b] = 'T';
}
}
cout<<"nTraps generated..... Press any key to continuen";
getch();
}
这是完整的代码…
#include <iostream>
#include <conio.h>
#include <ctime> //mainly to generate random numbers
#include <cstdlib> // for clear screen
using namespace std;
int trap_hit = 0; //0 => not stepped on trap
//1 => stepped on trap
char m; //where the player wants to move
int player_position = 0;
//The game board "." denotes empty square
char square [64] {'O','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','X'};
//place traps
void traps()
{
int a,b,r;
cout<<"Deciding placement of traps";
for (a = 1; a <= 8; a++)
{
r = clock();
cout<<"nPress any keyn";
getch();
cout<<"nPress any keyn";
getch();
r = r - clock();
b = r % 64;
if (square [b] == '.')
{
square[b] = 'T';
cout<<"Debug1"; //for debugging
}
}
cout<<"nTraps generated..... Press any key to continuen";
getch();
}
//to draw the board
void board()
{
system ("cls");
cout<<" Dungeon crawln";
cout<<"INSTRUCTIONS:use the keys "w", "s", "a" and "d"n to move up, down, left and right respectively.n Do not touch the Traps (T) or enemies (E)n";
cout<<"n "<<square [0]<<" "<<square [1]<<" "<<square [2]<<" "<<square [3]<<" "<<square [4]<<" "<<square [5]<<" "<<square [6]<<" "<<square [7]<<"n";
cout<<"n "<<square [8]<<" "<<square [9]<<" "<<square [10]<<" "<<square [11]<<" "<<square [12]<<" "<<square [13]<<" "<<square [14]<<" "<<square [15]<<"n";
cout<<"n "<<square [16]<<" "<<square [17]<<" "<<square [18]<<" "<<square [19]<<" "<<square [20]<<" "<<square [21]<<" "<<square [22]<<" "<<square [23]<<"n";
cout<<"n "<<square [24]<<" "<<square [25]<<" "<<square [26]<<" "<<square [27]<<" "<<square [28]<<" "<<square [29]<<" "<<square [30]<<" "<<square [31]<<"n";
cout<<"n "<<square [32]<<" "<<square [33]<<" "<<square [34]<<" "<<square [35]<<" "<<square [36]<<" "<<square [37]<<" "<<square [38]<<" "<<square [39]<<"n";
cout<<"n "<<square [40]<<" "<<square [41]<<" "<<square [42]<<" "<<square [43]<<" "<<square [44]<<" "<<square [45]<<" "<<square [46]<<" "<<square [47]<<"n";
cout<<"n "<<square [48]<<" "<<square [49]<<" "<<square [50]<<" "<<square [51]<<" "<<square [52]<<" "<<square [53]<<" "<<square [54]<<" "<<square [55]<<"n";
cout<<"n "<<square [56]<<" "<<square [57]<<" "<<square [58]<<" "<<square [59]<<" "<<square [60]<<" "<<square [61]<<" "<<square [62]<<" "<<square [63]<<"n";
}
//allow user to move
void pl_move()
{
char move_dir;
invalid_move:
cout<<"nPress w, a, s or d ...... ";
cin>>move_dir;
cin.clear();
cin.ignore(100000, 'n');
//allow the player to move
if (move_dir == 'w')
{
if (player_position < 8) //trying to move out of the board
{
cout<<"Invalid move";
goto invalid_move;
}
square [player_position] = '.';
player_position = (player_position - 8);
if (square [player_position] == 'T')
{
trap_hit = 1;
}
square [player_position] = 'O';
}
else if (move_dir == 's')
{
if (player_position >55 && player_position <64) //trying to move out of the board
{
cout<<"Invalid move";
goto invalid_move;
}
square [player_position] = '.';
player_position = (player_position + 8);
if (square [player_position] == 'T')
{
trap_hit = 1;
}
square [player_position] = 'O';
}
else if (move_dir == 'a')
{
if ((player_position % 8) == 0) //trying to move out of the board
{
cout<<"Invalid move";
goto invalid_move;
}
square [player_position] = '.';
player_position = (player_position - 1);
if (square [player_position] == 'T')
{
trap_hit = 1;
}
square [player_position] = 'O';
}
else if (move_dir == 'd')
{
if ((player_position % 8) == 7) //trying to move out of the board
{
cout<<"Invalid move";
goto invalid_move;
}
square [player_position] = '.';
player_position = (player_position + 1);
if (square [player_position] == 'T')
{
trap_hit = 1;
}
square [player_position] = 'O';
}
else
{
cout<<"nInvalid move";
goto invalid_move;
}
}
int main()
{
traps();
while(trap_hit == 0 && player_position != 63)
{
board();
pl_move();
}
board();
if (trap_hit == 1)
{
cout<<"nYou have stepped on a trap. You lose!";
}
else if (player_position == 63)
{
cout<<"nYou have retrieved the treasure and won!";
}
getch();
return 0;
}
您需要检查您的数组边界和b
值的有效性。
b
显然应该是:
(伪代码):
MinBound <= b <= MaxBound
在你的情况下,你可以这样做:
void traps()
{
int a,b,r;
cout<<"Deciding placement of traps";
for (a = 1; a <= 8; a++)
{
r = clock();
cout<<"nPress any keyn";
getch();
cout<<"nPress any keyn";
getch();
cout<<"f1"; //debug
r = clock() - r; //Edited to fix negetive values
cout<<"f2";
b = r % 64;
cout<<"f3";
//if (square [b] == '.')
//{
if(b < 0 || b>=64) //Validation Here
continue;
square[b] = 'T';
cout<<"Debug1";
//}
}
cout<<"nTraps generated..... Press any key to continuen";
getch();
}
相关文章:
- 为"adjacent"变量赋值时出现问题
- C++中的赋值发生,尽管右侧出现异常
- 赋值运算符在我制作类模板时使代码崩溃
- 复制赋值构造函数中的aligned_alloc内存块在释放时崩溃
- C++初始化复制构造函数中的列表赋值,并在复制构造函数中崩溃
- 为结构字段赋值时程序崩溃
- C++ 赋值崩溃
- 在 malloc 之后为数组赋值会导致崩溃
- 类成员作为对另一个的引用:赋值运算符中的崩溃
- 赋值运算符在 xcode 4 中崩溃,在MSVS2010中运行良好
- C++程序在为指针的2D数组赋值后崩溃
- memset导致std::string赋值崩溃
- 向量赋值崩溃
- 给数组赋值时崩溃
- 通过不返回引用来防止下标操作符的赋值会导致运行时崩溃
- c++在使用类方法给成员赋值时每次都会崩溃
- visualstudio-当给太多动态分配的数组赋值时程序崩溃-C++
- 是否赋值代码会导致崩溃
- 将一个glm::vec3常量引用赋值给另一个glm::vec3时奇怪的崩溃
- 自定义复制赋值操作符使程序崩溃(c++)