Thread: passing a false boolean value into functions, then return it as true to stop program?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    passing a false boolean value into functions, then return it as true to stop program?

    I want to pass a boolean value, bool gameover(false), into the function check_winning_condition, and then pass it to function
    ask_rematch. Then in ask_rematch, if the user's input is "no", then return the boolean gameover as true to stop the game.

    How do I do this? Or is there a better alternative to this? Am I making things harder?

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    void show_board(char board[][3]);
    
    
    
    
    
    
    int ask_rematch(char board[][3], bool gameover)
    {   string rematch;
        std::cout << "Would you like a rematch? yes/no \n";
        std::cin >> rematch;
        if ( rematch == "yes")
        {
             board[0][0] = '1'; board[0][1] = '2'; board[0][2] = '3';
             board[1][0] = '4'; board[1][1] = '5'; board[1][2] = '6';
             board[2][0] = '7'; board[2][1] = '8'; board[2][2] = '9';
        }
        else if ( rematch == "no")
        {
            gameover = true;
            return gameover;
        }
    
    
    }
    
    
    int check_winning_condition(char board[][3],string player[],bool gameover)
    {
        if ((board[0][0] ==  'X' && board[0][1] == 'X' && board[0][2] == 'X')||
            (board[1][0] ==  'X' && board[1][1] == 'X' && board[1][2] == 'X')||
            (board[2][0] ==  'X' && board[2][1] == 'X' && board[2][2] == 'X')||
            (board[0][0] ==  'X' && board[1][0] == 'X' && board[2][0] == 'X')||
            (board[0][1] ==  'X' && board[1][1] == 'X' && board[2][1] == 'X')||
            (board[0][2] ==  'X' && board[1][2] == 'X' && board[2][2] == 'X')||
            (board[2][0] ==  'X' && board[1][1] == 'X' && board[0][2] == 'X')||
            (board[0][0] ==  'X' && board[1][1] == 'X' && board[2][2] == 'X'))
            {
                std::cout << player[0] << " WINS!\n";
                show_board(board);
                ask_rematch(board,gameover);
                return gameover;
    
    
            }
            else if
           ((board[0][0] ==  'O' && board[0][1] == 'O' && board[0][2] == 'O')||
            (board[1][0] ==  'O' && board[1][1] == 'O' && board[1][2] == 'O')||
            (board[2][0] ==  'O' && board[2][1] == 'O' && board[2][2] == 'O')||
            (board[0][0] ==  'O' && board[1][0] == 'O' && board[2][0] == 'O')||
            (board[0][1] ==  'O' && board[1][1] == 'O' && board[2][1] == 'O')||
            (board[0][2] ==  'O' && board[1][2] == 'O' && board[2][2] == 'O')||
            (board[2][0] ==  'O' && board[1][1] == 'O' && board[0][2] == 'O')||
            (board[0][0] ==  'O' && board[1][1] == 'O' && board[2][2] == 'O'))
            {
                std::cout << player[1] << " WINS!\n";
                show_board(board);
                ask_rematch(board,gameover);
                return gameover;
    
    
            }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }
    
    
    void get_player_names(string player[])
    {   string player1, player2;
        std::cout << "Player 1's name: ";
        std::getline(cin, player1);
        std::cout << "\n";
        std::cout << "Player 2's name: ";
        std::getline(cin, player2);
        std::cout << "\n";
    
    
        player[0] = player1;
        player[1] = player2;
    
    
    }
    
    
    int  game_start(char board[][3],string player[])
    {
        bool gameover(false);
    
    
    
    
    
    
        int turn = 0; //players' turn
    
    
        do{
    
    
                    char mark;
        show_board(board);
        std::cout << player[turn] << "'s move.\n";
        std::cout << "Pick the number to mark. \n";
        std::cin  >> mark;
    
    
    
    
    
    
            for ( int row=0;row<3;row++ )
            {
                for (int column=0;column<3;column++)
                {
                    if(turn == 0 && mark == board[row][column] && mark != 'O')
                    { board[row][column] = 'X';
                      turn = 1;
                      }
                    else if (turn == 1 && mark == board[row][column] && mark != 'X')
                    { board[row][column] = 'O';
                      turn = 0;
                      }
                }
    
    
             }
             check_winning_condition(board,player,gameover);
    
    
    
    
        }
        while(!gameover);
    
    
    }
    
    
    void show_board(char board[][3])
    {
        for (int row=0;row<3;row++)
        {
            for (int column=0;column<3;column++)
            {
                std::cout << board[row][column] << " ";
            }
            std::cout << endl << endl;
        }
    }
    
    
    int main()
    {
        char board[3][3] = { {'1','2','3'} ,
                             {'4','5','6'} ,
                             {'7','8','9'}  };
       string player[2];
       get_player_names(player);
       game_start(board,player);
    
    
       return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It seems to me that ask_rematch should return a bool, and this value is then assigned to gameover. There is no need to pass gameover to ask_rematch because if you are calling ask_rematch, then obviously gameover must be false.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    but the bool gameover is declared in game_start, then passed to check_winning_condition, then passed to ask_rematch, to change its value to true. sorry I still can't comprehend.

    even after I type "no", and make change the gameover from false to true, the loop still continues. Am I doing it right?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Meerul264
    but the bool gameover is declared in game_start, then passed to check_winning_condition, then passed to ask_rematch, to change its value to true. sorry I still can't comprehend.
    If you want to do it that way, then you should pass gameover by reference. Personally, I would prefer to return a value because it might not be necessary to have the gameover variable to begin with, depending on how you structure your code. Thus, this gives flexibility to either assign to the gameover variable in the caller, or directly use the function's return value.

    Quote Originally Posted by Meerul264
    even after I type "no", and make change the gameover from false to true, the loop still continues. Am I doing it right?
    No, because you are assigning to a copy of gameover in ask_rematch.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    If you want to do it that way, then you should pass gameover by reference.
    You mean to add the & before bool gameover? I had read this somewhere but was not sure how to use it.

    Code:
    int ask_rematch(char board[][3], bool &gameover)
    Code:
    int check_winning_condition(char board[][3],string player[],bool &gameover)
    After this it ran the way I want it to be. After I type "no", the program exits. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why the eof returns false when it should return true?
    By kulfon in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2011, 09:57 AM
  2. True or False
    By noob programmer in forum C Programming
    Replies: 9
    Last Post: 10-29-2008, 10:15 AM
  3. True / False
    By goran00 in forum C Programming
    Replies: 13
    Last Post: 03-14-2008, 03:26 PM
  4. Return True or False, Not Print
    By BB18 in forum C Programming
    Replies: 5
    Last Post: 10-10-2004, 02:51 PM
  5. True or false statments used in my return...
    By correlcj in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2002, 04:26 PM