Trouble With Chars

I have been having logic errors when trying to change a char from a number to an 'x' or 'o' (not number '0' but letter 'o'). Could anyone help with pointing out where I screwed up? It is supposed to ask the user to type in a number and press enter and would then fill in the number mentioned with an 'x' when the graph is drawn again. When the graph is redrawn the x is not replaced. This is supposed to be a tic tac toe game when I am finished. Thanks for any help!

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
  #include <iostream>
#include <sstream>
#include <stdlib.h>
#include <algorithm>
#include <cctype>
#include <string>
#include <time.h>

using namespace std;

char box1 = '1';
char box2 = '2';
char box3 = '3';
char box4 = '4';
char box5 = '5';
char box6 = '6';
char box7 = '7';
char box8 = '8';
char box9 = '9';

void user1_choice();
void user2_choice();
void draw_board();

int test_x();

int main() {
	draw_board();
	cout << "player 1, please enter the number in which you would like to place your 'x'" << endl;
	user1_choice();
	draw_board();
	
	return 0;
}

void user1_choice() {
	string user1_prompt;
	char user1_choice;
	stringstream convert;

	convert.clear();
	convert.str("");

	getline(cin, user1_prompt);

	if (user1_prompt.length() > 1 || user1_prompt.length() < 1) {

		convert << user1_prompt;
		convert >> user1_choice;

		if (user1_choice = '1' && box1 != 'x' && box1 != 'o') {
			box1 = 'x';
		}
		else if (user1_choice = 2 && box2 != 'x' && box2 != 'o') {
			box2 = 'x';
		}
		else if (user1_choice = 3 && box3 != 'x' && box3 != 'o') {
			box3 = 'x';
		}
		else if (user1_choice = 4 && box4 != 'x' && box4 != 'o') {
			box4 = 'x';
		}
		else if (user1_choice = 5 && box5 != 'x' && box5 != 'o') {
			box5 = 'x';
		}
		else if (user1_choice = 6 && box6 != 'x' && box6 != 'o') {
			box6 = 'x';
		}
		else if (user1_choice = 7 && box7 != 'x' && box7 != 'o') {
			box7 = 'x';
		}
		else if (user1_choice = 8 && box8 != 'x' && box8 != 'o') {
			box8 = 'x';
		}
		else if (user1_choice = 9 && box9 != 'x' && box9 != 'o') {
			box9 = 'x';
		}
		else {
			cout << "what went wrong?!" << endl;
			system("SLEEP");
		}
	}
}

void draw_board(){
	system("cls");
	cout << "       |       |" << endl;
	cout << "   " << box1 << "   |   " << box2 << "   |   " << box3 << endl;
	cout << "_______|_______|_______" << endl;
	cout << "       |       |" << endl;
	cout << "   " << box4 << "   |   " << box5 << "   |   " << box6 << endl;
	cout << "_______|_______|_______" << endl;
	cout << "       |       |" << endl;
	cout << "   " << box7 << "   |   " << box8 << "   |   " << box9 << endl;
	cout << "       |       |" << endl;
}
You forgot the second = sign to differentiate between assignment and comparison for equality. Your entire else-if block has = instead of == for the user1_choice check.
Thanks so much! I totaly forgot that you have to use == instead of =
It will not allow me to use == with chars...
'1' is a char, but 2 (and others) is an int.

1
2
3
4
5
6
		if (user1_choice == '1' && box1 != 'x' && box1 != 'o') {
			box1 = 'x';
		}
		else if (user1_choice == 2 && box2 != 'x' && box2 != 'o') {
			box2 = 'x';
		}



If they enter a single character, e.g '2' to mark box 2, the length of user prompt will be 1. You only run the if/else code if the length is greater or less than 1.

if (user1_prompt.length() > 1 || user1_prompt.length() < 1) {
I am getting the warning "warning C4553: '==' : operator has no effect; did you intend '='?" for every == sign relating to the chars, does anyone know how to fix this? (My updated code for void user1_choice() is:
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
void user1_choice() {
	string user1_prompt;
	int user1_decision;
	stringstream convert;

	convert.clear();
	convert.str("");

	getline(cin, user1_prompt);

	if (!(user1_prompt.length() > 1 && user1_prompt.length() < 1)) {

		convert << user1_prompt;
		convert >> user1_decision;

		if (user1_decision = 1 && box1 != 'x' && box1 != 'o') {
			box1 == 'x';
		}
		else if (user1_decision = 2 && box2 != 'x' && box2 != 'o') {
			box2 == 'x';
		}
		else if (user1_decision = 3 && box3 != 'x' && box3 != 'o') {
			box3 == 'x';
		}
		else if (user1_decision = 4 && box4 != 'x' && box4 != 'o') {
			box4 == 'x';
		}
		else if (user1_decision = 5 && box5 != 'x' && box5 != 'o') {
			box5 == 'x';
		}
		else if (user1_decision = 6 && box6 != 'x' && box6 != 'o') {
			box6 == 'x';
		}
		else if (user1_decision = 7 && box7 != 'x' && box7 != 'o') {
			box7 == 'x';
		}
		else if (user1_decision = 8 && box8 != 'x' && box8 != 'o') {
			box8 == 'x';
		}
		else if (user1_decision = 9 && box9 != 'x' && box9 != 'o') {
			box9 == 'x';
		}
		else {
			cout << "what went wrong?!" << endl;
			system("SLEEP");
		}
	}
}
You want the == in the if statement where you're checking equality. Not when you're assigning 'x' to a box number.

And if you want to check if something is a char, you need the single quotes. '1' etc.
Last edited on
Tnaks guys for helping and responding to my questions so quickly! Ill re-open the thread if I have another issue. Thanks!
Topic archived. No new replies allowed.