Reputation: 75
I'm trying to create a tic-tac-toe game where the user inputs a number (corresponding to the position he wants place his X or O), but the variable (Move) which receives the number stays at 0 no matter what the input is. Can you help me figure out what to fix so the variable actually receives what the user inputs? Here is the code for the function which receives the move:
int FDeclarations::GetMove(){
int Move;
std::cin >> Move;
return Move;}
Here is the code for the function which goes through the switch statement (none of the works since the variable "Move" is always at 0)
int FDeclarations::PlaceMove(){
switch (Move)
{
case(1):
if (turn == false) { TopLeft = 'x'; }
else { TopLeft = 'o'; }
break;
case(2):
if (turn = false) { TopMid = 'x'; }
else { TopMid = 'o'; }
break;
case(3):
if (turn = false) { TopRight = 'x'; }
else { TopRight = 'o'; }
break;
case(4):
if (turn = false) { MidLeft = 'x'; }
else { MidLeft = 'o'; }
break;
case(5):
if (turn = false) { MidMid = 'x'; }
else { MidMid = 'o'; }
break;
case(6):
if (turn = false) { MidRight = 'x'; }
else { MidRight = 'o'; }
break;
case(7):
if (turn = false) { BotLeft = 'x'; }
else { BotLeft = 'o'; }
break;
case(8):
if (turn = false) { BotMid = 'x'; }
else { BotMid = 'o'; }
break;
case(9):
if (turn = false) { BotRight = 'x'; }
else { BotRight = 'o'; }
break;
}
table();
return 0;
}
Here are my variable declarations:
class FDeclarations
{
public:
int PlaceMove();
int GetMove();
int CheckWin();
void table();
private:
bool turn = false;
int Move;
char TopLeft = '1';
char TopMid = '2';
char TopRight = '3';
char MidLeft = '4';
char MidMid = '5';
char MidRight = '6';
char BotLeft = '7';
char BotMid = '8';
char BotRight ='9';
bool XWin;
bool OWin;
};
Upvotes: 3
Views: 177
Reputation: 31
In FDeclarations::GetMove()
you need to set the private member of your class Move
to whatever the user will input instead of the local variable wich shadows the member one. So a quick fix will be:
int FDeclarations::GetMove(){
std::cin >> Move;
return Move;}
Upvotes: 2
Reputation: 2049
In your function
int FDeclarations::GetMove() {
int Move;
std::cin >> Move;
return Move;
}
You declare a new variable called Move
which is local to that function. This is different from the member variable Move
declared in the class. C++ will prefer to bind to the function-level variable.
Unless you use the return value of GetMove
to set the member variable Move
in code you've not shown us, then the member variable Move
will never change, causing your problem.
Upvotes: 3