Code does not print updated values?

Hi, I'm trying to make a game of TicTacToe (Naughts and Crosses) using C++ (obviously C++ haha), and my code does not seem to update the game board with O's or X's when someone makes a move. I assume this has something to do with local and global variables and the variables that are updated are only updated within the "make a move" function, and so my main function does not get access to the updated variables (even though I returned them), but I have tried to change it around and either end up with the same result, or compiler errors. So I've decided to stick with how it is in the code below, but the program still does not print the updated variables. Can somebody help me understand why(and whether it is the result of local/global variables or not)?

The code is currently incomplete and only contains the function for O's first move, I didn't continue once I discovered it does not update squares. I also understand that "BLANK" isn't the best of things to put as the squares...it's temporary. Symbols such as " - " or " " didn't seem to work, and a simple space (" ") would be confusing.

Sorry it's so long.

EDIT: one more question: would an enum be better than multiple strings for the squares? I forgot to try that out (and I have no time now, otherwise I would before posting this, sorry).

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

using namespace std;

void PlayGame();

string SQR1 = "BLANK";
string SQR2 = "BLANK";
string SQR3 = "BLANK";
string SQR4 = "BLANK";
string SQR5 = "BLANK";
string SQR6 = "BLANK";
string SQR7 = "BLANK";
string SQR8 = "BLANK";
string SQR9 = "BLANK";

int main()
{
    int startgame;

    cout << "Welcome! To start, press 1.\n";

    for (int i = 0; i < 2; i++)
    {
        cin >> startgame;

        if ( i == 1 )
        {
            if ( startgame != 1 )
            {
                cout << "Goodbye.";
                break;
            }
            else
            {
                PlayGame();
                break;
            }
        }

        if ( startgame == 1 )
        {
            PlayGame();
            break;
        }
        else
        {
            cout << "You did not type 1, type 1 to start or type anything else to exit the program.\n";
        }
    }
}
void PlayGame()
{
    string MakeMoveN();
    string MakeMoveC();

    cout << "Naughts go first (O's), then Crosses. To select a square, type a number from 1-9, 1 being top left.\n\n\n";

    cout << '\t' << SQR1 << '\t' << SQR2 << '\t' << SQR3 << endl;
    cout << '\t' << SQR4 << '\t' << SQR5 << '\t' << SQR6 << endl;
    cout << '\t' << SQR7 << '\t' << SQR8 << '\t' << SQR9 << endl << endl << endl;

    for ( int i = 0; i < 5; i++ )
    {
        MakeMoveN();
    }
}
string MakeMoveN()
{
    int picksquare;

    cout << "Naughts, select your square: ";
    cin >> picksquare;

    switch ( picksquare )
    {
        case 1:
        {
            string SQR1 = "O";
            break;
            return SQR1;
        }
        case 2:
        {
            string SQR2 = "O";
            break;
            return SQR2;
        }
        case 3:
        {
            string SQR3 = "O";
            break;
            return SQR3;
        }
        case 4:
        {
            string SQR4 = "O";
            break;
            return SQR4;
        }
        case 5:
        {
            string SQR5 = "O";
            break;
            return SQR5;
        }
        case 6:
        {
            string SQR6 = "O";
            break;
            return SQR6;
        }
        case 7:
        {
            string SQR7 = "O";
            break;
            return SQR7;
        }
        case 8:
        {
            string SQR8 = "O";
            break;
            return SQR8;
        }
        case 9:
        {
            string SQR9 = "O";
            break;
            return SQR9;
        }
        default:
        {
            cout << "Error, please type a number from 1-9.\n";
        }
    }

    return SQR1, SQR2, SQR3, SQR4, SQR5, SQR6, SQR7, SQR8, SQR9;
}
Last edited on
You seem to have problems with how the type identifiers are used. Here are a few errors which should be fixed:

On lines 55 and 56, you are not actually calling the functions, you are forward-declaring them locally. If you want to call them, remove the string from in front of the function.

In your switch function, you are declaring a new local variable, which will override the global variables (which I presume you meant to modify). Also, due to the break statements, the return values will never be reached.

On your return statement on line 138, you are misunderstanding the comma operator. Here, you will simply return SQR1. You cannot return multiple values from a function.



Here is some things I would do:
Make your strings into an array of strings, and make your functions take arrays of strings as parameters. This would mean rather than calling SQR1, SQR2, etc... you would call squares[0], squares[1], etc... (or whatever you call the array). This could mean that you replace your switch functions to something like the following:
1
2
3
4
5
6
7
8
9
10
11
12
// void, because you don't need to return anything
// take an array of strings
void makeMoveN(std::string *squares) {
    int pickSquare = 0;
    // ...
    while (pickSquare < 1 || pickSquare > 9) {
        std::cout << "Enter square number [1-9]: ";
        std::cin >> pickSquare;
    }

    square[pickSquare - 1] = "O";
}


This is a start, hope this helps!
Thanks for the suggestions, they are useful. Only problem though is I wanted to avoid using arrays due to not having learnt about them yet at that point in the book I am using to make this game (prac Q at end of chapter said to make this), I moved ahead and learnt arrays, but at the point of having to make this game I hadn't, so I wanted to avoid them. I can use them if I must though.

Thank you.
(sorry if this post isn't allowed (bumping rules and such that exist on similar sites), but I figured it's better than making a new topic)

So I finally got time to retry this project to make it work, and I came up with a working game. There is only one problem. I have put the moves into functions so as to reduce the number of lines of code, but as a result the squares don't update properly. The square will update after the first turn, but then revert to original on second turn. I'm bad at explaining this so I'll show what happens when I run the program (not exact words but basically what happens):

Naughts, select square: 1
// player picks square 1, letters bold so can be seen easier

O Square2 Square3
Square4 Square5 Square6
Square7 Square8 Square9

Crosses, select square: 5
// player picks square 5, letters bold so can be seen easier

Square1 Square2 Square3
Square4 X Square6
Square7 Square8 Square9

Can anybody help me work out how to keep the squares updated without having to get rid of the separate functions for moves? I have tried to shorten the program by cutting out what isn't affecting this, but it is still very long, too long to post here without exceeding the maximum length limit (the program is 507 lines long). Still too long if I minimise the irrelevant parts and copy only what I think is relevant. Can you still help without the code? Sorry if it's inconvenient.

EDIT: Got it working, just changed the squares to global variables (I half can't believe I forgot to do that haha).
Last edited on
Topic archived. No new replies allowed.