Weird program crash

Why does my code keep crashing, when I enter a character or any number greater than 33x33? Shouldn't the NOT cmd (!) prevent it from happening; it's very strange. Unless 33*33 represents something maybe?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
using namespace std;

int playagain();

int main()
{
int board[3][3],x,y,loopcount=0;
char dispboard[3][3];
int PlayerA=79, PlayerB=88;
int i=0;
int winchecker=0, checkx=0, checky=0, leftdiag=0, rightdiag=0;
bool win=0;
cout<<"Player A=X, Player B=O"<<endl;
cout<<"[X,Y]"<<endl;
cout<<"[0,0]\t[1,0]\t[2,0]\n";
cout<<"[0,1]\t[1,1]\t[2,1]\n";
cout<<"[0,2]\t[1,2]\t[2,2]\n\n";


	while(loopcount<9 & win!=1)
	{
	i++;
	//to set board
	cout<<"CURRENT BOARD";
	for (y=0;y<3; y++)
		{
			cout<<endl<<"----------------------"<<endl;;
			for (x=0;x<3; x++)
			{
			if (loopcount==0) {board[x][y]=0;}
			dispboard[x][y]=board[x][y];
			cout<<dispboard[x][y]<<"\t";
		}
		}
	cout<<endl<<"----------------------"<<endl;

	//game play
	{
	do{
	if ((i % 2)!=0) {cout<<"Player A\n";loopcount++;}
	else {cout<<"Player B\n";loopcount++;}
	cin>>x; cin>>y;
	cout<<endl;
	if ((x>2 or x<0) or (y>2 or y<0) or (board[x][y]!=0)){cout<<"Error\n"; loopcount--;}
	if (board[x][y]==0)
	{
		if ((i % 2)!=0) {board[x][y]=PlayerA;}
		else {board[x][y]=PlayerB;}
	}
	else {x=5;}
	
	// to determine winner-vertical
	for (checkx=0;checkx<3;checkx++)
	{
	for (checky=0;checky<3;checky++)
	{
	winchecker+=board[checkx][checky];
	if (winchecker==(3*PlayerA)){cout<<"winner-player A"; winchecker=0; win=1; break;}
	if (winchecker==(3*PlayerB)){cout<<"winner-player B"; winchecker=0; win=1; break;}
	}
	winchecker=0;
	}
	
	// to determine winner-horizontal
	for (checky=0;checky<3;checky++)
	{
	for (checkx=0;checkx<3;checkx++)
	{
	winchecker+=board[checkx][checky];
	if (winchecker==(3*PlayerA)){cout<<"winner-player A"; winchecker=0; win=1; break;}
	if (winchecker==(3*PlayerB)){cout<<"winner-player B"; winchecker=0; win=1; break;}
	}
	winchecker=0;
	}
	
	//to determine winner-left diagnal (left top start) & right diagnal
	{
	leftdiag=board[0][0]+board[1][1]+board[2][2];
	rightdiag=board[2][0]+board[1][1]+board[0][2];
	
	if (((leftdiag==(3*PlayerA))) or ((rightdiag==(3*PlayerA)))){cout<<"winner-player A"; winchecker=0; win=1; break;}
	if (((leftdiag==(3*PlayerB))) or ((rightdiag==(3*PlayerB)))){cout<<"winner-player B"; winchecker=0; win=1; break;} 
	}
			
	if ((win!=1) && loopcount==9){cout<<"No Winners Idiots";}
	}while((x>2 or x<0) or (y>2 or y<0));
	}
	}	
	cout<<endl;
	playagain();
	return 0;
}


int playagain()
{
	int playagain;
	cout<<"Press 1 to play again, anything else to exit\n";
	cin>>playagain;
	while(playagain==1)
	{
	main();
	}
return 0;
}
Last edited on
Hi,

First stop, compile with all the warning levels on:

In function 'int main()': 21:17: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses] 
In function 'int playagain()': 103:7: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic]


Can you figure out the first one?

With the second one: never do that, it could lead to a stack overflow. Use loops instead.

Your code could do with some indenting, hopefully you can configure your editor to do it automatically.
Last edited on
I've removed the play again int with the "int main" loop chunk of code from my program and put parenthesis around all number clauses, yet the program still continues to crash.

For some reason, the compiler says there are "0" errors detected. Maybe 1089 represents a digit beyond the int allowable amount?

Maybe I need to somehow clear the data or use a pointer... i've got no idea though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
using namespace std;

//int playagain();

int main()
{
int playagain=0;
int board[3][3],x,y,loopcount=0;
char dispboard[3][3];
int PlayerA=79, PlayerB=88;

int i=0;
int winchecker=0, checkx=0, checky=0, leftdiag=0, rightdiag=0;
bool win=0;



do{
cout<<"Player A=X, Player B=O"<<endl;
cout<<"[X,Y]"<<endl;
cout<<"[0,0]\t[1,0]\t[2,0]\n";
cout<<"[0,1]\t[1,1]\t[2,1]\n";
cout<<"[0,2]\t[1,2]\t[2,2]\n\n";


	while((loopcount<9) & (win!=1))
	{
	i++;
	//to set board
	cout<<"CURRENT BOARD";
	for (y=0;y<3; y++)
		{
			cout<<endl<<"----------------------"<<endl;;
			for (x=0;x<3; x++)
			{
			if (loopcount==0) {board[x][y]=0;}
			dispboard[x][y]=board[x][y];
			cout<<dispboard[x][y]<<"\t";
		}
		}
	cout<<endl<<"----------------------"<<endl;

	//game play
	{
	do{
	if ((i % 2)!=0) {cout<<"Player A\n";loopcount++;}
	else {cout<<"Player B\n";loopcount++;}
	cin>>x; cin>>y;
	cout<<endl;
	if (((x>2) or (x<0)) or ((y>2) or (y<0)) or ((board[x][y])!=0)){cout<<"Error\n"; loopcount--;}
	if ((board[x][y])==0)
	{
		if ((i % 2)!=0) {(board[x][y])=PlayerA;}
		else {(board[x][y])=PlayerB;}
	}
	else {x=5;}
	
	// to determine winner-vertical
	for (checkx=0;checkx<3;checkx++)
	{
	for (checky=0;checky<3;checky++)
	{
	winchecker+=board[checkx][checky];
	if (winchecker==(3*PlayerA)){cout<<"winner-player A"; winchecker=0; win=1; break;}
	if (winchecker==(3*PlayerB)){cout<<"winner-player B"; winchecker=0; win=1; break;}
	}
	winchecker=0;
	}
	
	// to determine winner-horizontal
	for (checky=0;checky<3;checky++)
	{
	for (checkx=0;checkx<3;checkx++)
	{
	winchecker+=board[checkx][checky];
	if (winchecker==(3*PlayerA)){cout<<"winner-player A"; winchecker=0; win=1; break;}
	if (winchecker==(3*PlayerB)){cout<<"winner-player B"; winchecker=0; win=1; break;}
	}
	winchecker=0;
	}
	
	//to determine winner-left diagnal (left top start) & right diagnal
	{
	leftdiag=board[0][0]+board[1][1]+board[2][2];
	rightdiag=board[2][0]+board[1][1]+board[0][2];
	
	if (((leftdiag==(3*PlayerA))) or ((rightdiag==(3*PlayerA)))){cout<<"winner-player A"; winchecker=0; win=1; break;}
	if (((leftdiag==(3*PlayerB))) or ((rightdiag==(3*PlayerB)))){cout<<"winner-player B"; winchecker=0; win=1; break;} 
	}
			
	if ((win!=1) && loopcount==9){cout<<"No Winners Idiots";}
	}while((x>2 or x<0) or (y>2 or y<0));
	}
	}	
	cout<<endl;
	cin>>playagain;
}while (playagain==1);
	
	
	return 0;
}
Last edited on
Hi,

I think you have missed the point with the first warning: use && for AND logical operations. The & does a binary operation which is not what you want.

27
28
29
while((loopcount<9) && (win!=1))
	{
	i++;


..... put parenthesis around all number clauses, .....


So you could get rid of those now.

Not sure if that is all the problems you may have, your code is fairly difficult to follow. Try using a debugger, there should be a GUI one in your IDE.
Topic archived. No new replies allowed.