tic tac toe code bug?

So I was writing this TIC-TAC-TOE program which lets a user select a cell in the table to be marked as `x` but after the original value in the table is changed the value will be displayed as int 120 no matter what is entered. Could anyone point out the problem pls)




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
#include<iostream>
using namespace std;



				   
   
int cell[9];

	void table();
	int choice();
	char bar=(char)179;
	
  int main() {
  	
  	char usersymbol,pcsymbol;
  	usersymbol='x';pcsymbol='o';
  	
  	for(int i=1;i<=9;i++){cell[i]=i;}
  	table();
  	///////////
  	
  	int uservalue=choice();
  	if(uservalue==cell[uservalue]) {
               cell[uservalue]=usersymbol;
               table();
                   }
  	  	 	
  }
  
   void table() {
   	int j=0;
  for(int i=1;i<=9;i++)  {
  	cout<<cell[i]<<"|";j++;
  	if(j%3==0){
  		cout<<endl<<"------"<<endl;
	  }
    }
  }
  
 int choice() {
  	int userchoice;
  	cout<<"your turn human"<<endl;
  	cin>>userchoice;
  	return userchoice;
  }
   
for(int i=1;i<=9;i++){cell[i]=i;}

You access your array out of bounds. Valid index is 0-8
em i changed the program accordingly and still get the same result.
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
#include<iostream>
using namespace std;



int cell[8];

	void table();
	int choice();
	char bar=(char)179;
	
  int main() {
  	
  	char usersymbol,pcsymbol;
  	usersymbol='x';pcsymbol='o';
  	
  	for(int i=0;i<=8;i++){cell[i]=i+1;}
  	table();
  	///////////
  	
  	int uservalue=choice();
  	if(uservalue==cell[uservalue-1]){cell[uservalue-1]=usersymbol;table();}
  	
  	
  	
  }
  
   void table() {
   	int j=0;
  for(int i=0;i<=8;i++)  {
  	cout<<cell[i]<<"|";j++;
  	if(j%3==0){
  		cout<<endl<<"------"<<endl;
	  }
    }
  }
  
 int choice() {
  	int userchoice;
  	cout<<"your turn human"<<endl;
  	cin>>userchoice;
  	return userchoice;
  }
 
Try running your code in cpp.sh (click the gear on the right-hand side of your code, refresh the page if you don't see the gear icon) and see the errors + warnings that the compiler prints out.

http://cpp.sh/

If this is tic-tac-toe, that means you are trying to make a 3x3 board, which is 9 tiles. You have only made the size of your cell array to be 8.

int cell[8]; --> int cell[9];
Arrays are 0-indexed, so cell[9]'s valid indices are [0], [1], ..., [8].

___________________________________

Also, as the (now deleted, but correct) post below me said, I'll paraphrase. x's ASCII value is 120. When you assign a char to an int, that number is casts to an int ('x' becomes 120). The design of your code is a bit odd because you are mixing ints (1-9) with chars ('x'/'o') in the same array.

With your current design, in your table() function, I would change it a bit by adding an if statement that says "if the character is an x or an o, print the character, otherwise print the 1-9 value"

1
2
3
4
5
6
7
8
9
10
11
12
13
void table() {
  int j=0;
  for (int i = 0; i <= 8; i++) {
    if (cell[i] == 'x' || cell[i] == 'o')
  	    cout << (char)cell[i] << "|";
  	else
  	    cout << cell[i] << "|";
  	j++;
  	if(j%3 == 0){
  	  cout << endl <<"------" << endl;
    }
  }
}


Execution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
output on right
1|2|3|
------
4|5|6|
------
7|8|9|
------
your turn human
4
1|2|3|
------
x|5|6|
------
7|8|9|
------
Last edited on
Ganado,

everything you said makes sense.Thank you for putting in the time else i d have been upon this for a long time. Also could you recollect for me how you ascended from this "noob" state to a rly good programmer! Again thanks a lot)

thomas 1965 sorry for being so dumb about me not understanding what you said xd
thankssssss
Last edited on
Topic archived. No new replies allowed.