problem with segmentation fault

#include <iostream>
#include <cstdlib>
#include <string>
#ifndef TIC_TAC_TOE_H
#define TIC_TAC_TOE_H

class tic_tac_toe {
public:
void prnt();
void get_move();
void winner();
} TICTACTOE;
#endif
//class declaration

using namespace std;
char player1='X';
char player2='O';
int inputcounter=0;
const int a=3,b=3;
char board[a][b]={ {'1','2','3'}, {'4','5','6'}, {'7','8','9'} };
//declarations

void tic_tac_toe::prnt() {
int x;
cout << " 1 2 3\n"
<< " *=*=*=*\n";
for(x=0;x<=2;x++) {
cout << x+1 << " |" << board[x][0] << "|" << board[x][1] << "|" << board[x][2] << "|" << endl
<< " *=*=*=*\n";
}
}
//function will print the tic tac toe box

void tic_tac_toe::get_move() {
int x=1;
int row[20], column[20], row1[20], column1[20]; bool tf=false, ft=false;
for(inputcounter=0;inputcounter<=8;inputcounter++) {
while(tf!=true) {
cout << "\nPlayer1: enter the row and column of desired move: ";
cin >> row[x] >> column[x];
//will ask the user to input the row and column

if(inputcounter==0) {
if(board[row[x]-1][column[x]-1]==board[row1[x]-1][column1[x]-1]) {
cout << "\nError-tile already occupied"; tf=false;
}
else {board[row[x]-1][column[x]-1]='X'; x++; tf=true;
} TICTACTOE.prnt();
}
//will output the "error" line if the condition was met else it outputs the result after the input or where the user wishes to have his/her move
if(inputcounter>=1 && inputcounter<=8) {
if( (board[row[x]-1][column[x]-1]==board[row1[x-1]-1][column1[x-1]-1]) || (board[row[x-1]-1][column[x-1]-1]==board[row[x]-1][column[x]-1]) || (board[row[x-1]-1][column[x-1]-1]==board[row[x]-1][column[x]-1]) ){
cout << "\nError-tile already occupied"; tf=false;
}
else {board[row[x]-1][column[x]-1]='X'; x++; tf=true;}
TICTACTOE.prnt();}
// same with line 51, except only runs within the values 1 to 8 of inputcounter
while (ft!=true) {
cout << "\nPlayer2: enter the row and column of desired move: ";
cin >> row1[x] >> column1[x];

//the segmentation fault is happening within these lines
//please help me find out

if(inputcounter==0) {
if( (board[row[x]-1][column[x]-1]==board[row1[x]-1][column1[x]-1]) ) {
cout << "\nError-tile already occpuied" ; ft=false;
}
else {board[row1[x]-1][column1[x]-1]='O'; x++; ft=true;}
} TICTACTOE.prnt();
}
if(inputcounter>=1 && inputcounter<=8) {
if( (board[row[x]-1][column[x]-1]==board[row1[x]-1][column[x]-1]) || (board[row1[x-1]-1][column1[x-1]-1]==board[row1[x]-1][column1[x]-1]) || (board[row1[x]-1][column1[x]-1]==board[row[x]-1][column[x]-1]) ) {
cout << "\nError-tile already occupied"; ft=false;
}
else {board[row1[x]-1][column1[x]-1]='O'; ft=true; }
} TICTACTOE.prnt();
}
if(inputcounter==9) {
TICTACTOE.winner();
}
}
}

void tic_tac_toe::winner() {
int x=0;
for(x=0;x<3;x++) {
if(board[x][0]==board[x][1] && board[x][0]==board[x][2]) {
if(board[x][0]==player1) {
cout <<"Player1 wins";
}
else {
cout << "Player2 wins";
}
}
else {cout << "Draw!";}
}
}

int main() {
cout << "This is a Tic Tac Toe Game made into a program. \nLegend:Player1='X' and Player2='O'\n Please Enjoy!";
system("pause>0"); system("cls");
TICTACTOE.prnt();
TICTACTOE.get_move(); system("pause>0"); system("cls");
TICTACTOE.winner(); system("pause>0"); system("cls");
system("pause>0");
return 0;
}
I'm getting a segmentation fault on line 45:
35
36
37
38
39
40
41
42
43
44
45
void tic_tac_toe::get_move() {
int x=1;
int row[20], column[20], row1[20], column1[20]; bool tf=false, ft=false;
for(inputcounter=0;inputcounter<=8;inputcounter++) {
while(tf!=true) {
cout << "\nPlayer1: enter the row and column of desired move: ";
cin >> row[x] >> column[x];
//will ask the user to input the row and column

if(inputcounter==0) {
if(board[row[x]-1][column[x]-1]==board[row1[x]-1][column1[x]-1]) {

What do you think row1[x] and column1[x] contain?
Hint: They were never initialized, so they contains garbage. Trying to use a garbage index to subscript into board is guaranteed to cause a segmentation fault.

I really don't understand the purpose of the row, column, row1 and column1 arrays. They appears to just add unnecessary complexity (and errors) to your program.

You have been asked previously to use code tags. PLEASE DO SO.
http://www.cplusplus.com/forum/beginner/166375/#msg837658
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
I will not respond further until you apply code tags.
Last edited on
Here's an updated code of the program, my problem now is how should i put in the code for that. That is in lines 92 up to 153. And please do tell me if i still lack anything to edit as a post here on www.cplusplus.com
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <cstdlib>
#include <string>
#ifndef TIC_TAC_TOE_H
#define TIC_TAC_TOE_H
//header files declaration

class tic_tac_toe {
public:
void prnt();
void get_move();
void winner();
};
#endif
tic_tac_toe TICTACTOE;
//class declaration

using namespace std;
char player1='X';
char player2='O';
int inputcounter=0;
const int a=3,b=3;
char board[a][b]={ {'1','2','3'}, {'4','5','6'}, {'7','8','9'} };
//declarations

void tic_tac_toe::prnt() {
int x;
cout << "   1 2 3\n"
     << "  *=*=*=*\n";
for(x=0;x<=2;x++) {
cout << x+1 << " |" << board[x][0] << "|" << board[x][1] << "|" << board[x][2] << "|" << endl
     << "  *=*=*=*\n";
	}
}
//function will print the tic tac toe box 

void tic_tac_toe::get_move() {
int x=1;
int row[20]={'\0','\0'}, column[20]={'\0','\0'}, row1[20]={'\0','\0'}, column1[20]={'\0','\0'};
/*  row[20] and column[20] are for the storage of the inputs of player1
while row1[20] and column1[20] are for the storage of the inputs of player2 */
bool tf=false, ft=false, check=false;
while(!check) {
while(!tf) {
cout << "\nPlayer1: enter the row and column of desired move: ";
cin >> row[x] >> column[x];
//will ask the user to input the row and column

	if(inputcounter==0) {
		if((row[x]>3 || row[x]<=0) || (column[x]>3 || column[x]<=0) ) {
			cout << "\nError-tile already occupied"; tf=false; }
		}
		board[row[x]-1][column[x]-1]=player1; tf=true;
		TICTACTOE.prnt();
		
	//will output the "error" line if the condition was met else it outputs the result after the input or where the user wishes to have his/her move
	if(inputcounter>=1 && inputcounter<=8) {
		TICTACTOE.winner();
	for(;inputcounter>=0;inputcounter--) {
	if( (board[row[inputcounter]-1][0]==board[row[inputcounter]-1][1]) || (board[row[inputcounter]-1][1]==board[row[inputcounter]-1][2]) || (board[row[inputcounter]-1][0]==board[row[inputcounter]-1][2]) ){
		cout << "\nError-tile already occupied"; tf=false; }
		else {board[row[x]-1][column[x]-1]=player1; tf=true; TICTACTOE.winner();}
		TICTACTOE.prnt();}
			}
		}
	TICTACTOE.winner();
	// same with line 51, except only runs within the values 1 to 8 of inputcounter
	while (!ft && inputcounter!=8) {
	cout << "\nPlayer2: enter the row and column of desired move: ";
	cin >> row1[x] >> column1[x];
	if(inputcounter==0) {
		if((row[x]>3 || row[x]<=0) || (column[x]>3 || column[x]<=0) ) {
			cout << "\nError-tile already occupied"; tf=false; }
		}
		if( (board[row[x]-1][column[x]-1]==board[row1[x]-1][column1[x]-1]) ) {
			cout << "\nError-tile already occpuied" ; ft=false;
		}
		else {board[row1[x]-1][column1[x]-1]=player2; x++; ft=true;}
		 system("cls");TICTACTOE.prnt();
	if(inputcounter>=1 && inputcounter<=7) {
	for(;inputcounter>=0;inputcounter--) {
	if( (board[row[inputcounter]-1][0]==board[row[inputcounter]-1][1]) || (board[row[inputcounter]-1][1]==board[row[inputcounter]-1][2]) || (board[row[inputcounter]-1][0]==board[row[inputcounter]-1][2]) ){
		cout << "\nError-tile already occupied"; ft=false; }
		else {board[row[x]-1][column[x]-1]=player2; x++; ft=true; TICTACTOE.winner();}
		system("cls");TICTACTOE.prnt();}
				}
			}
		}
		tf=false; ft=false;
} 

void tic_tac_toe::winner ()
{
	string congrats="\nWe have a winner!";
	string nothing="\nNext Move : \n";
	
	//checks every row
	for (int x=0;x<3;x++)
	{
			if (board[x][0]==board[x][0+1] && board[x][0]==board[x][2])
			{
				if (board[x][0]==player1)
					cout <<"\nPlayer1 wins!";
				else if (board[x][0]==player2)
					cout <<"\nPlayer2 wins!";
			}
	}
	
	//checks every column
	for (int x=0;x<3;x++)
	{
			if (board[x][0]==board[x][1] && board[x][0]==board[x][2] && board[x][1]==board[x][2])
			{
				if (board[x][0]==player1)
					cout <<"\nPlayer1 wins!";
				else if (board[x][0]==player2)
					cout <<"\nPlayer2 wins!";
			}
	}
	
	//checks left to right diagonal
	if (board[0][0]==board[1][1]&&board[0][0]==board[2][2]){
				if (board[0][0]==player1)
					cout <<"\nPlayer1 wins!";
				else if (board[0][0]==player2)
					cout <<"\nPlayer2 wins!";
	}
	//checks right to left diagonal
	if (board[2][0]==board[1][1]&&board[2][0]==board[0][2]) {
				if (board[2][0]==player1)
					cout <<"\nPlayer1 wins!";
				else if (board[2][0]==player2)
					cout <<"\nPlayer2 wins!";
			}
	if(inputcounter==9) {


//This is where i check if it's a draw or not
	if( (board[0][0]!=board[0][1] &&board[0][0]!=board[0][2] &&board[0][1]!=board[0][2])
	 || (board[1][0]!=board[1][1] && board[1][0]!=board[1][2] && board[1][1]!=board[1][2])
	 	|| (board[2][0]!=board[2][1] && board[2][0]!=board[2][2] && board[2][1]!=board[2][2])
		  || (board[0][0]!=board[1][1] && board[0][0]!=board[2][2] && board[1][1]!=board[2][2])
			|| (board[0][2]!=board[1][1] && board[0][2]!=board[2][0] && board[1][1]!=board[2][0])
			 || (board[0][1]!=board[1][1] && board[0][1]!=board[2][1] && board[1][1]!=board[2][1])
			  || (board[0][2]!=board[1][2] && board[0][2]!=board[2][2] && board[1][2]!=board[2][2]) ) {
		cout << "Draw!";
		}



	}
	cout << nothing;
}

int main() {
cout << "This is a Tic Tac Toe Game made into a program. \nLegend:Player1='X' and Player2='O'\n Please Enjoy!";
system("pause>0"); system("cls");

for(inputcounter=0;inputcounter<=8;inputcounter++) {
TICTACTOE.prnt();
TICTACTOE.get_move(); system("pause>0"); system("cls");
}
system("pause>0");
return 0;
}
Last edited on
my problem now is how should i put in the code for that.

I don't know what that means. What are you referring to?

Lines 139-147 can be simplified. You've already checked if player 1 or player 2 is the winner.
If you have no winner, and input_count is 9, then the only possibility is a draw. That very ugly if statement is unnecessary.

Where you check for a winner, if you find a winner, you still drop through and reach line 152 which prompts for next move. winner should be a bool function. Return true if a winner found, or if a draw (game should not continue). Return false to indicate the game is not over.

When I ran your game, I entered 1,1 for player 1 and 3,3 for player 2 and your program went into an infinite loop displaying "Next move".



Topic archived. No new replies allowed.