Tic Tac Toe - passing variables -
Apr 5, 2014 at 12:24am UTC
My error is happening on line 78 under the [m], it says identifier is undefined. I'm watching tutorial videos to get me through it and I have it set up the same way as them, but I'm running into an error that they seem to not run into
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 <string>
using namespace std;
char board[9];
int showBoard();
bool moveIsValid(int m);
int main()
{
//declare local variables
string playerOne;
string playerTwo;
int whoseTurn = 1;
int move;
//assign values to playing board
board[0] = '1' ;
board[1] = '2' ;
board[2] = '3' ;
board[3] = '4' ;
board[4] = '5' ;
board[5] = '6' ;
board[6] = '7' ;
board[7] = '8' ;
board[8] = '9' ;
//get players names
cout << "What is the name of player 1?" << endl;
getline(cin, playerOne);
cout << "What is the name of player 2?" << endl;
getline(cin, playerTwo);
//do this until the player enters a valid move
do
{
//show board
showBoard();
//Tell what player to move
if (whoseTurn == 1)
{
cout << playerOne << ": It's your turn." << endl;
}
else
{
cout << playerTwo << ": It's your turn." << endl;
}
//get the move
cout << "Enter the number of the spot where you would like to move." << endl;
cin >> move;
} while (moveIsValid(move) != true );
//change whose turn it is
switch (whoseTurn)
{
case (1) :
{
whoseTurn = 2;
board[move] = 'X' ;
break ;
}
case (2) :
{
whoseTurn = 1;
board[move] = 'O' ;
}
}
//show board updated
showBoard();
}
int showBoard()
{
bool moveIsValid(int m);
{
if (board[m] != 'X' && board[m] != 'O' )
{
return true ;
}
else
{
return false ;
}
}
{
cout << endl;
cout << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "--+---+--" << endl;
cout << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "--+---+--" << endl;
cout << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << endl;
}
}
Apr 5, 2014 at 1:05am UTC
It's because you have a function in a function. Put the moveIsValid
function and code outside of the showBoard
function. Then delete the extra pair of curly-braces.
Apr 5, 2014 at 1:35am UTC
When I do that I get a error on line 2 saying 'expected a declaration'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
bool moveIsValid(int m);
{
if (board[m] != 'X' && board[m] != 'O' )
{
return true ;
}
else
{
return false ;
}
}
int showBoard()
{
cout << endl;
cout << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << "--+---+--" << endl;
cout << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << "--+---+--" << endl;
cout << board[6] << " | " << board[7] << " | " << board[8] << endl;
cout << endl;
}
Apr 5, 2014 at 1:54am UTC
Delete the semicolon after bool moveIsValid(int m)
.
Topic archived. No new replies allowed.