Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:00:50+00:00 2026-05-26T21:00:50+00:00

If statement acting weird. For some reason if in the IF statement if (

  • 0

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; doesn’t work.

Basically the else in the first once it gets used goes to the seconds else in the second if statement. Why?

#include <iostream>
using namespace std;

// Initialize Array to 9 elements.
// Set array elements 1 to 9 to specify locations for placement of X and O.
char ticTacBoard[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};

// gameState will return 1 to 4. #1. Player X has won, #2. Player Y has won, #3. Draw, #4. Game Not Finished.
int gameState = 0;
int choosePositionX = 0;
int choosePositionO = 0;
int totalMoves = 0;

// Functions declared to run game.
// Checks for winners, draw and if game should continue.
int checkGameState (int gameState);
// Takes in users moves.
void makeMove ();
// Resets board when game finished.
void reset ();
// Prints the board to the user.
void printBoard ();


int main()
{
    cout << "Welcome to Tic Tac Toe!" << endl << endl
         << "The goal of Tic Tac Toe is to be the first player to get three in a row on the grid." << endl << endl
         << "Please select 1 through 9 to select placement for your turn."<< endl << endl;

    int gameState = 4;

    printBoard();

    while ( gameState == 4)
    {
        cout << "Player X please choose a position: ";
        cin >> choosePositionX;
        cout << endl;
        makeMove();
        totalMoves = totalMoves + 1;
        printBoard();
        gameState = checkGameState(gameState);
        if (gameState != 4)
        {
            if ( gameState == 1 )
            {
                cout << "Player X has won the game!" << endl << endl;
                break;
            }
            if ( gameState == 2 )
            {
                cout << "Player Y has won the game!" << endl << endl;
                break;
            }
            if ( gameState == 3 )
            {
                cout << "The game is a DRAW!" << endl << endl;
                break;
            }
        }
        cout << "Player O please choose a position: ";
        cin >> choosePositionO;
        cout << endl;
        makeMove();
        totalMoves = totalMoves + 1;
        printBoard();
        gameState = checkGameState(gameState);
        if (gameState != 4)
        {
            if ( gameState == 1 )
            {
                cout << "Player X has won the game!" << endl << endl;
                break;
            }
            if ( gameState == 2 )
            {
                cout << "Player Y has won the game!" << endl << endl;
                break;
            }
            if ( gameState == 3 )
            {
                cout << "The game is a DRAW!" << endl << endl;
                break;
            }
        }
    }

    char playAgain;

    cout << "Would you like to play again? y/n: ";
    cin >> playAgain;
    cout << endl;

    if (playAgain == 'y')
    {
        reset();
    }
    else if (playAgain == 'n')
    {
        cout << endl;
        cout << "Thanks for playing!!";
        return 0;
    }

    return 0;
}

void reset()
{
    for (int i = 0; i < 9; i++)
    {
        ticTacBoard[i] = 49 + i;
    }

    gameState = 0;
    choosePositionX = 0;
    choosePositionO = 0;
    totalMoves = 0;

    main();
}

int checkGameState(int gameState)
{

////////////// CHECK FOR X /////////////////

    // Check Rows
    if(ticTacBoard[0] == 'X' && ticTacBoard[1] == 'X' && ticTacBoard[2] == 'X')
    {
        return 1;
    }
    if(ticTacBoard[3] == 'X' && ticTacBoard[4] == 'X' && ticTacBoard[5] == 'X')
    {
        return 1;
    }
    if(ticTacBoard[6] == 'X' && ticTacBoard[7] == 'X' && ticTacBoard[8] == 'X')
    {
        return 1;
    }

    // Check Columns
    if(ticTacBoard[0] == 'X' && ticTacBoard[3] == 'X' && ticTacBoard[6] == 'X')
    {
        return 1;
    }
    if(ticTacBoard[1] == 'X' && ticTacBoard[4] == 'X' && ticTacBoard[7] == 'X')
    {
        return 1;
    }
    if(ticTacBoard[2] == 'X' && ticTacBoard[5] == 'X' && ticTacBoard[8] == 'X')
    {
        return 1;
    }

    // Check Diagonally
    if(ticTacBoard[0] == 'X' && ticTacBoard[4] == 'X' && ticTacBoard[8] == 'X')
    {
        return 1;
    }
    if(ticTacBoard[2] == 'X' && ticTacBoard[4] == 'X' && ticTacBoard[6] == 'X')
    {
        return 1;
    }

////////////// CHECK FOR O /////////////////

    // Check Rows
    if(ticTacBoard[0] == 'O' && ticTacBoard[1] == 'O' && ticTacBoard[2] == 'O')
    {
        return 2;
    }
    if(ticTacBoard[3] == 'O' && ticTacBoard[4] == 'O' && ticTacBoard[5] == 'O')
    {
        return 2;
    }
    if(ticTacBoard[6] == 'O' && ticTacBoard[7] == 'O' && ticTacBoard[8] == 'O')
    {
        return 2;
    }

    // Check Columns
    if(ticTacBoard[0] == 'O' && ticTacBoard[3] == 'O' && ticTacBoard[6] == 'O')
    {
        return 2;
    }
    if(ticTacBoard[1] == 'O' && ticTacBoard[4] == 'O' && ticTacBoard[7] == 'O')
    {
        return 2;
    }
    if(ticTacBoard[2] == 'O' && ticTacBoard[5] == 'O' && ticTacBoard[8] == 'O')
    {
        return 2;
    }

    // Check Diagonally
    if(ticTacBoard[0] == 'O' && ticTacBoard[4] == 'O' && ticTacBoard[8] == 'O')
    {
        return 2;
    }
    if(ticTacBoard[2] == 'O' && ticTacBoard[4] == 'O' && ticTacBoard[6] == 'O')
    {
        return 2;
    }

    // After 9 moves we call a Draw. Since we should have a winner before that.
    if ( totalMoves >= 9)
    {
        return 3;
    }

    // Otherwise CONTINUE
    else
        return 4;
}

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;
        }
    }
}

void printBoard()
{
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            cout << ticTacBoard[3 * y + x] << " ";
        }

        cout << endl;
    }
    cout << endl;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:00:50+00:00Added an answer on May 26, 2026 at 9:00 pm

    You forgot an else that connects your two ifs. You need to use:

    else if ( choosePositionO )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OpenGL is acting very strangely for some reason. In my subclass of NSOpenGLView, I
Problem statement: It is necessary for me to write a code, whether which before
Problem Statement - Would like to know if particular web app user is active
Which statement will be executed after continue or break ? for(int i = 0;
I have a switch statement acting as a menu, in this I am trying
This statement below gets me some data I need. select TankName, COUNT(*) as Tanks,
Which statement should I use in php scripts? Echo or Print? What is faster
There is a statement in the foudations of Qt Development book that goes as
Problem statement: An image A is projected through a projector, goes through a microscope
Switch statement fallthrough is one of my personal major reasons for loving switch vs.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.