If statement acting weird. For some reason if in the IF statement `if ( choosePositionX )` goes to else which tells the user the other player already made the move it goes into the next if `if ( choosePositionO )` and prints out the else in that one. How can i make it so once it goes to else in either if statement that it just breaks out and continues down. it seems that break; doesnt work. So basically my problem is that the other else in the other statement follows the first one and so on.

void makeMove()
{
    if (choosePositionX)
    {
        if (ticTacBoard[choosePositionX - 1] != 'X' && ticTacBoard[choosePositionX - 1] != 'O' )
        {
            ticTacBoard[choosePositionX - 1] = 'X';
        }
        else
        {
            cout << "Player O has already made this move" << endl << endl;
            // keep same moves for counter draw
            totalMoves = totalMoves - 1 + 1;
        }
    }

    if (choosePositionO)
    {
        if (ticTacBoard[choosePositionO - 1] != 'X' && ticTacBoard[choosePositionO - 1] != 'O' )
        {
            ticTacBoard[choosePositionO - 1] = 'O';
        }
        else
        {
            cout << "Player X has already made this move" << endl << endl;
            // keep same moves for counter draw
            totalMoves = totalMoves - 1 + 1;
        }
    }
}

Recommended Answers

All 6 Replies

Instead of if (choosePositionO) us else instead.

Yeah but then it doesnt go into the the choosePositionO statement....

What are the values of choosePositionX choosePositionO ticTacBoard[choosePositionX - 1 ticTacBoard[choosePositionO - 1 ?
That will tell you exactly why the IF is doing what it's doing.

Put output statements at key places in the code to see if the values are as expected. You will also be able to see why your IFs aren't doing what they are supposed to.

I figured it out!!!

Basically i have a cin for choosePositionX and cin for choosePositionO. If the user cin for choosePositionX and the if statement is true it runs the code in it. When the user cin for choosePositionO and the if statement is true it runs for the code for it. BUT in the event for those ifs choosePositionx and choosePositionO if its not equal then it should go to else and say this player blah blah already made this move. BUT my code ends up outputting both elses so it says it for both X and O.


The problem lays in the void makeMove() function

I did not ask for a verbal explanation of what the code does. I can see that for myself.

Reread my last post, do what's suggested, and study the values. See what those values do in your function.

If your function is correct but the values are bad, you need to find out where the bad values get into the variables. More output statements.

It wasnt a function error i just had to set choosePositionX and choosePositionO to 0 so it wouldnt go into other ifs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.