Tic tac toe outputting weird characters

I'm the millionth person to do this, but I'm making a tic tac toe game (currently unfinished) and have been using a class to store the board information about the squares. Every time the program runs however, it is printing a board with very strange symbols when it should initially print it with white space. My main function sets all of the squares as white space, but when it prints the board, it gives me pretty much everything but white space. What am I doing wrong?

Note: Program is NOT finished. Please don't bother with other various holes unless I'm doing something extremely wrong.

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
166
167
168
169
170
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

int checkwin();

char player1_mark;
char player2_mark;

//********BOARDSTATUS CLASS*********
//Holds information about board.
//

class Boardstatus
{
    char A1;
    char A2;
    char A3;
    char B1;
    char B2;
    char B3;
    char C1;
    char C2;
    char C3;

public:

    void setsquarestatus( int square, char status )
    {
        switch ( square )
        {
        case 1:
            A1 = status;
            break;
        case 2:
            A2 = status;
            break;
        case 3:
            A3 = status;
            break;
        case 4:
            B1 = status;
            break;
        case 5:
            B2 = status;
            break;
        case 6:
            B3 = status;
            break;
        case 7:
            C1 = status;
            break;
        case 8:
            C2 = status;
            break;
        case 9:
            C3 = status;
            break;
        }
    }

    char getsquarestatus( int square )
    {
        switch ( square )
        {
        case 1:
            return A1;
            break;
        case 2:
            return A2;
            break;
        case 3:
            return A3;
            break;
        case 4:
            return B1;
            break;
        case 5:
            return B2;
            break;
        case 6:
            return B3;
            break;
        case 7:
            return C1;
            break;
        case 8:
            return C2;
            break;
        case 9:
            return C3;
            break;
        }
    }

    //End of public

};

//*********BOARDPRINT FUNCTION***********
//
//
void boardprint()
{
    Boardstatus BSTAT;
	system("cls");
	cout << "\n\n\tTic Tac Toe\n\n";

	cout << "Player 1 (" << player1_mark << ")  -  Player 2 (" << player2_mark << ")" << endl << endl;
	cout << endl;

	cout << "     |     |     " << endl;
	cout << "  " << BSTAT.getsquarestatus(1) << "  |  " << BSTAT.getsquarestatus(2) << "  |  " << BSTAT.getsquarestatus(3) << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << BSTAT.getsquarestatus(4) << "  |  " << BSTAT.getsquarestatus(5) << "  |  " << BSTAT.getsquarestatus(6) << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << BSTAT.getsquarestatus(7) << "  |  " << BSTAT.getsquarestatus(8) << "  |  " << BSTAT.getsquarestatus(9) << endl;

	cout << "     |     |     " << endl << endl;
}

int main()
{
//Start of initializing all squares as a blank
Boardstatus BSTAT;
BSTAT.setsquarestatus( 1, ' ' );
BSTAT.setsquarestatus( 2, ' ' );
BSTAT.setsquarestatus( 3, ' ' );
BSTAT.setsquarestatus( 4, ' ' );
BSTAT.setsquarestatus( 5, ' ' );
BSTAT.setsquarestatus( 6, ' ' );
BSTAT.setsquarestatus( 7, ' ' );
BSTAT.setsquarestatus( 8, ' ' );
BSTAT.setsquarestatus( 9, ' ' );
//End of square initialization

boardprint();

    do
    {
        cout << "Player 1, choose X or O: ";
        cin >> player1_mark;
        cin.clear();
        cin.sync();
        if ( player1_mark == 'X' )
        {
            player2_mark = 'O';
        }
        else if (player1_mark == 'O' )
        {
            player2_mark = 'X';
        }
        else
        {
            cout << "Invalid character. Please try again." << endl;
        }
    } while ( player1_mark != 'X' && player1_mark != 'O');

    boardprint();

    return 0;
}
Last edited on
You have two BSTAT objects, 1 in main, 1 in boardprint(). The object's values in boardprint are not initialized,thus you get weird values. These objects are not identical. You need to pass the object into board print as an argument.

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
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

int checkwin();

char player1_mark;
char player2_mark;

//********BOARDSTATUS CLASS*********
//Holds information about board.
//

class Boardstatus
{
    char A1;
    char A2;
    char A3;
    char B1;
    char B2;
    char B3;
    char C1;
    char C2;
    char C3;

public:

    void setsquarestatus( int square, char status )
    {
        switch ( square )
        {
        case 1:
            A1 = status;
            break;
        case 2:
            A2 = status;
            break;
        case 3:
            A3 = status;
            break;
        case 4:
            B1 = status;
            break;
        case 5:
            B2 = status;
            break;
        case 6:
            B3 = status;
            break;
        case 7:
            C1 = status;
            break;
        case 8:
            C2 = status;
            break;
        case 9:
            C3 = status;
            break;
        }
    }
//this functions job is to return information. it does not modify the object in any way so its good
//practice to make functions like these const
    char getsquarestatus( int square ) const 
    {
        switch ( square )
        {
        case 1:
            return A1;
            break;
        case 2:
            return A2;
            break;
        case 3:
            return A3;
            break;
        case 4:
            return B1;
            break;
        case 5:
            return B2;
            break;
        case 6:
            return B3;
            break;
        case 7:
            return C1;
            break;
        case 8:
            return C2;
            break;
        case 9:
            return C3;
            break;
        }
    }

    //End of public

};

//*********BOARDPRINT FUNCTION***********
//
//
//Function accepts a reference to the BSTAT object, a copy isn't necessary.
//and the object is made const because it shouldnt be modified in this function.
void boardprint(const Boardstatus& BSTAT)
{
    
	system("cls");
	cout << "\n\n\tTic Tac Toe\n\n";

	cout << "Player 1 (" << player1_mark << ")  -  Player 2 (" << player2_mark << ")" << endl << endl;
	cout << endl;

	cout << "     |     |     " << endl;
	cout << "  " << BSTAT.getsquarestatus(1) << "  |  " << BSTAT.getsquarestatus(2) << "  |  " << BSTAT.getsquarestatus(3) << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << BSTAT.getsquarestatus(4) << "  |  " << BSTAT.getsquarestatus(5) << "  |  " << BSTAT.getsquarestatus(6) << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << BSTAT.getsquarestatus(7) << "  |  " << BSTAT.getsquarestatus(8) << "  |  " << BSTAT.getsquarestatus(9) << endl;

	cout << "     |     |     " << endl << endl;
}

int main()
{
//Start of initializing all squares as a blank
Boardstatus BSTAT;
BSTAT.setsquarestatus( 1, ' ' );
BSTAT.setsquarestatus( 2, ' ' );
BSTAT.setsquarestatus( 3, ' ' );
BSTAT.setsquarestatus( 4, ' ' );
BSTAT.setsquarestatus( 5, ' ' );
BSTAT.setsquarestatus( 6, ' ' );
BSTAT.setsquarestatus( 7, ' ' );
BSTAT.setsquarestatus( 8, ' ' );
BSTAT.setsquarestatus( 9, ' ' );
//End of square initialization

boardprint(BSTAT);//put object in function.

    do
    {
        cout << "Player 1, choose X or O: ";
        cin >> player1_mark;
        cin.clear();
        cin.sync();
        if ( player1_mark == 'X' )
        {
            player2_mark = 'O';
        }
        else if (player1_mark == 'O' )
        {
            player2_mark = 'X';
        }
        else
        {
            cout << "Invalid character. Please try again." << endl;
        }
    } while ( player1_mark != 'X' && player1_mark != 'O');

    boardprint(BSTAT);//put object in function

    return 0;
}

Last edited on
Thank you SO much. This is really helpful. I'll post back here if I have any more problems.
I've run into another problem. My class Boardstatus is not behaving correctly and I'm not entirely sure what's wrong.

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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>

using namespace std;

bool checkwin();

char player1_mark;
char player2_mark;

//********BOARDSTATUS CLASS*********
//Holds information about board.
//

class Boardstatus
{

char A1;
char A2;
char A3;
char B1;
char B2;
char B3;
char C1;
char C2;
char C3;

public:

    int setsquarestatus( string square, char status )
    {
        int squarenumber = enumerator ( square );
        switch ( squarenumber )
        {
        case 1:
            A1 = status;
            break;
        case 2:
            A2 = status;
            break;
        case 3:
            A3 = status;
            break;
        case 4:
            B1 = status;
            break;
        case 5:
            B2 = status;
            break;
        case 6:
            B3 = status;
            break;
        case 7:
            C1 = status;
            break;
        case 8:
            C2 = status;
            break;
        case 9:
            C3 = status;
            break;
        }
    }

    char getsquarestatus( string square )
    {
        int squarenumber = enumerator(square);
        switch ( squarenumber )
        {
        case 1:
            return A1;
            break;
        case 2:
            return A2;
            break;
        case 3:
            return A3;
            break;
        case 4:
            return B1;
            break;
        case 5:
            return B2;
            break;
        case 6:
            return B3;
            break;
        case 7:
            return C1;
            break;
        case 8:
            return C2;
            break;
        case 9:
            return C3;
            break;
        }
    }

    //End of public

private:

    //This function acts similar to an enumerator, but instead returns a number 1-9 based
    //on what string x is entered so that the switch cases in get and setsquarestatus will
    //function properly (they require int or char to work)
    int enumerator( string x )
    {
       if ( x == 'A1' )
       {
           return 1;
       }

       else if ( x == 'A2' )
       {
           return 2;
       }

       else if (x == 'A3')
       {
           return 3;
       }

       else if (x == 'B1')
       {
           return 4;
       }

       else if (x == 'B2')
       {
           return 5;
       }

       else if (x == 'B3')
       {
           return 6;
       }

       else if (x == 'C1')
       {
           return 7;
       }

       else if (x == 'C2')
       {
           return 8;
       }

       else if (x == 'C3')
       {
           return 9;
       }
    }



};


In my class, I have a setsquarestatus and getsquarestatus function. These functions take a string as an argument (as well as the square status... ie ( A1, X )), but I am using a switch case to return the value of the inputted square. To do this, I created a pseudo-enumerator function into private that returns a number 1-9 based on what square string is inputted. This allows the switch cases to work. However, I am getting a whole slew of errors and I don't even know where to begin???!

D:\Program Files\CodeBlocks\TicTacToe\TicTacToe\main.cpp|111|error: no match for 'operator==' in 'x == 16689'|

I get a whole mountain of errors regarding strings that I don't understand.

I also know there are probably better ways of doing what I'm trying to do, but I'm just trying to make this work. If you have a better idea, please let me know.


I will post all my code upon request.
Last edited on
Topic archived. No new replies allowed.