T. Long
T. Long

Reputation: 223

Can't end while loop (C++)

I can't seem to end this while loop and I have tried every single way I know but it's still not working. The variable "contGame" just kept changing to "true" even if I have already changed it to "false". Can you guys help me! Note that this inside a class. Here is my code:

while (contGame == true)
    {
        if (turn == 1)
        {
            cout << Board._player1Name <<"'s turn!!\n";
            X = getMoveX();
            Y = getMoveY();

            Board.Board[X][Y] = player1Sign;
            Board.printBoard();
            turn = turn + 1;
            Board.checkGame(contGame, player1Sign, player2Sign);
            if (checkGame(contGame, player1Sign, player2Sign) == false)
            {
                bool contGame = false;
                cout << contGame << endl;
            }
        } 
        cout << contGame << endl;
        if(turn == 2)
        {
            cout << Board._player2Name <<"'s turn!!\n";
            X = getMoveX();
            Y = getMoveY();

            Board.Board[X][Y] = player2Sign;
            Board.printBoard();
            turn = turn - 1;
            Board.checkGame(contGame, player1Sign, player2Sign);
            if (checkGame(contGame, player1Sign, player2Sign) == false)
            {
                int contGame = 1;
                cout << contGame << endl;
            }
        }
    }  

And this is checkGame:

bool contGameA = true;
for (int k = 0; k < 3; k++)
    {

        if (Board[k][0] == Board[k][1] && Board[k][1] == Board[k][2] && Board[k][2] == player1Sign)
        {
            cout << "\\\\" << _player1Name <<" wins!////\n";
            contGameA = false;
        }
        else if (Board[k][0] == Board[k][1] && Board[k][1] == Board[k][2] && Board[k][2] == player2Sign)
        {
            cout << "\\\\" << _player2Name << " wins!////\n";
            contGameA = false;
        }
    }

for (int r = 0; r < 3; r++)
    {
        if (Board[0][r] == Board[1][r] && Board[1][r] == Board[2][r] && Board[2][r] == player1Sign)
        {
            cout << "\\\\" << _player1Name << " wins!////\n";
            contGameA = false;
        }
        else if (Board[0][r] == Board[1][r] && Board[1][r] == Board[2][r] && Board[2][r] == player2Sign)
        {
            cout << "\\\\" << _player2Name << " wins!////\n";
            contGameA = false;
        }   
    }

if (Board[0][0] == Board[1][1] && Board[1][1] == Board[2][2] && Board[2][2] == player1Sign)
    {
        cout << "\\\\" << _player1Name << " wins!////\n";
        contGameA = false;
    } else if (Board[0][0] == Board[1][1] && Board[1][1] == Board[2][2] && Board[2][2] == player2Sign)
    {
        cout << "\\\\" << _player2Name << " wins!////\n";
        contGameA = false;
    }

if (Board[0][2] == Board[1][1] && Board[1][1] == Board[2][0] && Board[2][0] == player1Sign)
    {
        cout << "\\\\" << _player1Name << " wins!////\n";
        contGameA = false;
    } else if (Board[0][2] == Board[1][1] && Board[1][1] == Board[2][0] && Board[2][0] == player2Sign)
    {
        cout << "\\\\" << _player2Name << " wins!////\n";
        contGameA = false;
    }
return contGameA;

Upvotes: 2

Views: 99

Answers (3)

Twinkle
Twinkle

Reputation: 524

The scope of the variables only falls within the brackets when declared inside a any loop. Remove the bool and int and just put

if (checkGame(contGame, player1Sign, player2Sign) == false)
{
contGame = false;
}

Upvotes: 0

You keep declaring new variables with the name contGame in nested scopes, and modifying those. These are entirely separate variables, which (in their scope) hide the outer variable contGame. It's that outer one on whose value the while loop depends.

Remove the bool and int from the lines:

bool contGame = false;

// and

int contGame = 1;

to have these lines affect the outer contGame variable. Also note that 1 is converted to true.

Upvotes: 1

J&#233;r&#244;me
J&#233;r&#244;me

Reputation: 8066

You are actually creating contGame variable in the nested scope of your if statement and this is hiding contGame variable declared in the outer scope. As a result you never change the outer contGame variable

e.g.

if (checkGame(contGame, player1Sign, player2Sign) == false)
{
    // bool contGame = false; should be:
    contGame = false;
    cout << contGame << endl;
}

and

if (checkGame(contGame, player1Sign, player2Sign) == false)
{
    // int contGame = 1; should be
    contGame = true;
    cout << contGame << endl;
}

Upvotes: 2

Related Questions