tic tac toe program not running

So I got motivated to write a 2 Player tic tac toe program,where a user enters a column no. to change it into X or O but the program doesnt after the first user enters his choice.It d be nice to get to know why.I have not written the won or not algorithm yet.I wouldnt mind criticisms either if the program is ridiculously long or something like that.thank you

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
 #include<iostream>

using namespace std;

char array[10];


void drawtable();
void choices(int player);

int main() {
	
	int roll;roll=1;
	
array[0]='1';
array[1]='2';
array[2]='3';
array[3]='4';
array[4]='5';
array[5]='6';
array[6]='7';
array[7]='8';
array[8]='9';
	
	drawtable();
	
	do{
	
	choices(roll);
	roll++;
	drawtable();
	choices(roll);
	roll++;
	drawtable();
	}while(roll<=9);
	
	
	
	
}

void drawtable() {
	
	
int j;
int i=1;
	
	for(j=0;j<=8;j++) {
		
		cout<<array[j]<<"|";
		
		if(i%3==0) {
			cout<<endl;
			cout<<"------\n";
		}
		i++;
		
	}
	
	
}

void choices(int player) {
	char choice;
	char usersymbol;
		if(player%2!=0){
			cout<<"player1: ";
			cin>>choice;
			usersymbol='x';
				}
		else {  cout<<"player2: ";
	  		    cin>>choice;
				usersymbol='o'; 	}
	  		 

int a=1; // to keep the for loop going on
	for(int i=0;a=1;i++) {						
//for-loop to overwrite the char value selected by the user in the table 
		if(array[i]==choice) {
			
			array[i]==usersymbol;
			a=0;
		}
		else {a=1;}
	}   
	
} 
Your choices() function is wrong at line 81 and line 77 (these errors may be hard to detect if you didn't get compiler warnings ('==' vs '=' ).
By the way, you will get a segmentation fault if you do not check whether the input is a valid number.

Here a suggestion:
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
void choices(int player) {
	char choice;
	char usersymbol;

	while (true) {
	
	 	if (player % 2 != 0) {
			cout << "player1: ";
			cin >> choice;
			usersymbol = 'x';
		} else {
			cout << "player2: ";
		  	cin >> choice;
			usersymbol = 'o'; 
		}
		  		 
                //for-loop to overwrite the char value selected by the user in the table 
		for (int i = 0; i < 9; i++) {	
			if ( array[i] == choice ) {
				array[i] = usersymbol;
				return;    // !here the unique function return!
			}
		}
		// No valid number was entered
		cout << "Sorry, wrong field. Try again.\n";
	}
}
oh it works now! Thanks a lot you saved my day. Plus you just reminded me that i have not understood the while(......) part cos i have no idea what would happen if i were to enter false instead of true in that condition part, ya i ll google that and get that cleared in my headd.

Again thanks a lot for your help))
Last edited on
Topic archived. No new replies allowed.