Console returning wrong char (tic tac toe)

Good evening. I decided to learn coding for the first time a week ago, but I'm already having some issues. I'm trying to make a basic Tic Tac Toe game where the user can insert an 'x' by selecting one of the displayed numbers. I'm still in the first step and for some reason even if I type 5, the only block to become is 'x' is always the first one (1).

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

int main(){
	char a = '1', b = '2', c = '3', d = '4', e = '5', f= '6', g = '7', h = '8', i = '9';
	int n;
	
    cout << "_____________________________"<< endl;
	cout << "         |         |         "<< endl;
	cout << "    "<< a <<"    |    "<<b<<"    |    "<< c<< "    "<< endl;
	cout << "_________|_________|_________"<< endl;
	cout << "         |         |         "<< endl;
	cout << "    "<<d<<"    |    "<<e<<"    |    "<<f<<"    "<< endl;
	cout << "_________|_________|_________"<< endl;
	cout << "         |         |         "<< endl;
	cout << "    "<<g<<"    |    "<<h<<"    |    "<<i<<"    "<< endl;
	cout << "_________|_________|_________"<< endl;
	
	cout << "Player1:";
	cin >> n;
	if (n = 1){
		a = 'x';
	} else if (n = 2){
		b = 'x';
	}else if (n=3){
		c = 'x';
	} else if (n=4){
		d = 'x';
	} else if (n=5){
		e = 'x';
	} else if (n = 6){
		f = 'x';
	} else if (n=7){
		g = 'x';
	}else if (n=8){
		h = 'x';
	} else if (n = 9){
		i = 'x';
	};
	
	    cout << "_____________________________"<< endl;
	cout << "         |         |         "<< endl;
	cout << "    "<< a <<"    |    "<<b<<"    |    "<< c<< "    "<< endl;
	cout << "_________|_________|_________"<< endl;
	cout << "         |         |         "<< endl;
	cout << "    "<<d<<"    |    "<<e<<"    |    "<<f<<"    "<< endl;
	cout << "_________|_________|_________"<< endl;
	cout << "         |         |         "<< endl;
	cout << "    "<<g<<"    |    "<<h<<"    |    "<<i<<"    "<< endl;
	cout << "_________|_________|_________"<< endl;
	
	return 0;
Last edited on
= is an assignment (setting the value of a variable).
== is a comparison for equality; are the values of two things the same?

You are using = when you require == inside the 'if' tests.
You have done it rather a lot of times.
Last edited on
It seems, that you have mistakenly confound the assignment operator ( = ) with the comparing operator ( == ).

And, welcome aboard, sailing through rough sea of C++ :-)

*edit*
You should also learn some basics about how to use ASCII code. E.g, the characters '0'...'9' have the ASCII-codes 48...37. There you need some arithmetic to convert them. For converting ASCII to integer, you could hence subtract the char '0', and for converting a digit to ASCII-character you could add the '0' character.
Last edited on
Topic archived. No new replies allowed.