can someone check this? nightmare with functions

this is the error:

build/Debug/Cygwin_4.x-Windows/main.o:main.cpp:(.rdata$.refptr.gameIsNotOver[.refptr.gameIsNotOver]+0x0): undefined reference to `gameIsNotOver'
build/Debug/Cygwin_4.x-Windows/main.o:main.cpp:(.rdata$.refptr.input[.refptr.input]+0x0): undefined reference to `input'


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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h> /* exit(), EXIT_SUCCESS */
#include "main.h"
std::vector<std::string> board(9);
// Shows instructions to the player

void showInstructions() {
    std::cout << "Enter a number to place an X there. Get three in a row to win! /n";
    std::cout << "|1| |2| |3|/n";
    std::cout << "|4| |5| |6|/n";
    std::cout << "|7| |8| |9|/n";
    std::cout << "Press any key to continue! /n";
    std::cin.ignore();
}
// Shows the board

void showBoard(std::vector<std::string> board) {
    std::cout << "|";
    std::cout << board[0];
    std::cout << "| |";
    std::cout << board[1];
    std::cout << "| |";
    std::cout << board[2];
    std::cout << "|";
    std::cout << std::endl;
    std::cout << "|";
    std::cout << board[3];
    std::cout << "| |";
    std::cout << board[4];
    std::cout << "| |";
    std::cout << board[5];
    std::cout << "|";
    std::cout << std::endl;
    std::cout << "|";
    std::cout << board[6];
    std::cout << "| |";
    std::cout << board[7];
    std::cout << "| |";
    std::cout << board[8];
    std::cout << "|";
    std::cout << std::endl;
}
// Takes input from player

int playerInput() {
    int input;
    std::cout << "Input your move!";
    std::cin >> input;
    input - 1;
    return input;
}
// updates board after player moves

std::vector<std::string> updateBoard(int input, std::vector<std::string> &board) {
    if (input == 0) {
        if (board[0] == " ") {
            board[0] = "X";
        } else {
            std::cout << "Please input the number of a blank square.";
            playerInput();
        }
    }
    if (input == 1) {
        if (board[1] == " ") {
            board[1] = "X";
        } else {
            std::cout << "Please input the number of a blank square.";
            playerInput();
        }
    }
    if (input == 2) {
        if (board[2] == " ") {
            board[2] = "X";
        } else {
            std::cout << "Please input the number of a blank square.";
            playerInput();
        }
    }
    if (input == 3) {
        if (board[3] == " ") {
            board[3] = "X";
        } else {
            std::cout << "Please input the number of a blank square.";
            playerInput();
        }
    }
    if (input == 4) {
        if (board[4] == " ") {
            board[4] = "X";
        } else {
            std::cout << "Please input the number of a blank square.";
            playerInput();
        }
    }
    if (input == 5) {
        if (board[5] == " ") {
            board[5] = "X";
        } else {
            std::cout << "Please input the number of a blank square.";
            playerInput();
        }
    }
    if (input == 6) {
        if (board[6] == " ") {
            board[6] = "X";
        } else {
            std::cout << "Please input the number of a blank square.";
            playerInput();
        }
    }
    if (input == 7) {
        if (board[7] == " ") {
            board[7] = "X";
        } else {
            std::cout << "Please input the number of a blank square.";
            playerInput();
        }
    }
    if (input == 8) {
        if (board[8] == " ") {
            board[8] = "X";
        } else {
            std::cout << "Please input the number of a blank square.";
            playerInput();
        }
    }
    return board;
}

// If player wins

void playerWins() {
    char playerResponse;
    std::cout << "You win!";
    std::cin.get(playerResponse);
    if (playerResponse == 'y') {
        main();
    }
    if (playerResponse == 'n') {
        exit(EXIT_SUCCESS);
    } else {
        std::cout << "Please input your response again.";
        std::cin.get(playerResponse);
    }
}
// If player loses

void playerLoses() {
    char playerResponse;
    std::cout << "You lose!";
    std::cin.get(playerResponse);
    if (playerResponse == 'y') {
        main();
    }
    if (playerResponse == 'n') {
        exit(EXIT_SUCCESS);
    } else {
        std::cout << "Please input your response again.";
        std::cin.get(playerResponse);
    }
}
// If player ties

void playerTies() {
    char playerResponse;
    std::cout << "You tied!";
    std::cin.get(playerResponse);
    if (playerResponse == 'y') {
        main();
    }
    if (playerResponse == 'n') {
        exit(EXIT_SUCCESS);
    } else {
        std::cout << "Please input y/n.";
        std::cin.get(playerResponse);
    }
}

std::vector<std::string> computerMove(std::vector<std::string> &board) {
    if (board[0] == "" && board[1] == "X" && board[2] == "X") {
        board[0] = "O";
    } else if (board[0] == "X" && board[1] == "" && board[2] == "X") {
        board[1] = "O";
    } else if (board[0] == "X" && board[1] == "X" && board[2] == "") {
        board[2] = "O";
    } //etc
    return board;
}
//Checks if computer or player has won

bool checkForWinner(std::vector<std::string> board, bool gameIsNotOver) {
    if (board[0] == "X" && board[1] == "X" && board[2] == "X") {
        gameIsNotOver = false;
        return gameIsNotOver;
        playerWins();
    }
    if (board[0] == "O" && board[1] == "O" && board[2] == "O") {
        gameIsNotOver = false;
        return gameIsNotOver;
        playerLoses();
    }//etc
    // check for a tie
    int squares_filled = 0;
    for (int i = 0; i != 9; ++i) {
        if (board[i] != "") {
            ++squares_filled;
        }
    }
    if (squares_filled == 9) {
        playerTies();
    } else if (
            squares_filled != 9) {
        squares_filled = 0;
    } else {
        return gameIsNotOver;
    }
}

void gameLoop(bool gameIsNotOver) {
    int counter = 0;
    if (counter == 0) {
        showInstructions();
        ++counter;
    }
    while (gameIsNotOver == true) {
        showBoard(board);
        playerInput();
        updateBoard(input, board);
        checkForWinner(board, gameIsNotOver);
        computerMove(board);
        showBoard(board);
        checkForWinner(board, gameIsNotOver);
    }

}

int main() {
    gameLoop(gameIsNotOver);
    return 0;
}


// main.h

#ifndef MAIN_H
#define	MAIN_H
extern int input;
extern bool gameIsNotOver;
int main();
void showInstructions();
void showBoard(std::vector<std::string> board);
int playerInput();
std::vector<std::string> updateBoard(int input, std::vector<std::string> &board);
void playerWins();
void playerLoses();
void playerTies();
std::vector<std::string> computerMove(std::vector<std::string> &board);
bool checkForWinner(std::vector<std::string> board, bool gameIsNotOver);
void gameLoop(bool gameIsNotOver);
#endif 
In main.h, you have declared input and gameIsNotOver as extern, but you haven't actually defined them anywhere.

I think your problem is actually coming from not understanding functions. One things I see is on line 197 and 202, where you try to do something after having returned from the function. After you've returned, the function is finished; you cannot continue to do things after that point.

Additionally, checkForWinner() seems to be returning whether or not the game has ended yet, though on lines 231 and 234 you don't store the information or even try to use it.
That is pretty true, i've been struggling with functions in this program alot and finding out I know alot less about them then I thought.

And thanks I totally missed that I didn't define either input or gameIsNotOver.

I'll define those variables and would putting the call to playerWins() or playerLoses() before the return fix that problem?

I'll take a look at how checkForWinner is working and I was planning on adding some conditionals and storing that somehow but I guess I just kinda blew right by that.
btw thanks a ton for such a nice reply.
Last edited on
I'll define those variables and would putting the call to playerWins() or playerLoses() before the return fix that problem?


You would run those functions, and then execution would continue to the return statement and return the value, yes. If you don't know how to use a debugger, now would be a great time to learn as it will help you figure out how your code works.
Topic archived. No new replies allowed.