7

I have a function to return a bool:

bool restart()
{
    std::string answer;
    bool answered = false;
    while(answered == false)
    {
        cout << endl << endl << "Do you want to play again? y/n : ";
        cin >> answer;
        if(answer == "y" || answer == "Y" || answer == "1" || answer == "yes")
        {return true;}
        if(answer == "n" || answer == "N" || answer == "0" || answer == "no")
        {return false;}
    }
}

When I call it using:

cout << restart();

I get the output:

Do you want to play again? y/n : y
56

Can anyone see how to fix this strange problem? Thanks in advance.

My WIP code as it is now:

#include <iostream>
#include <cstdlib>

using namespace std;

void drawScreen(int grid[3][3]);                               //
int movef(int grid[3][3], bool playersMove);
int updateGrid(int grid[3][3], int move, bool playersMove);
bool hasWon(int grid[3][3]);
bool swapMover(bool playersMove);                              //
bool restart();
void endGame();

int main()
{
    int grid[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    bool appRunning = true, gameRunning = true, playersMove = true;
    int move;

    // tests //
    std::cout << restart();
    // tests //

    //while(appRunning == true)
    //{
    //    while(gameRunning == true)
    //    {
    //        drawScreen(grid);
    //        move = movef(grid, playersMove);
    //        grid[3][3] = updateGrid(grid, move, playersMove);
    //        drawScreen(grid);
    //        gameRunning = hasWon(grid);
    //        playersMove = swapMover(playersMove);
    //    }
    //    appRunning = restart();
    //}
    //endGame();
    }

    void drawScreen(int grid[3][3])
    {
        for(int i = 0; i < 3; i++)
        {
             for(int j = 0; j < 3; j++)
             {
             if(grid[i][j] == 10){cout << "X  ";if(j == 2){cout << endl << endl;}}
             if(grid[i][j] == 11){cout << "O  ";if(j == 2){cout << endl << endl;}}
             if(grid[i][j] != 10 && grid[i][j] != 11){cout << grid[i][j] << "  ";
             if(j == 2){cout << endl << endl;}}
             }
        }
   }

  int movef(int grid[3][3], bool playersMove)
  {
       return 0;
  }

  int updateGrid(int grid[3][3], int move, bool playersMove)
  {
       return 0;
  }

  bool hasWon(int grid[3][3])
  {
      return false;
  }

  bool swapMover(bool playersMove)
  {
       if(playersMove == true){return false;}
       if(playersMove == false){return true;}
  }

bool restart()
{
    std::string answer;
    bool answered = false;
    while(answered == false)
    {
       cout << endl << endl << "Do you want to play again? y/n : ";
       cin >> answer;
       if(answer == "y" || answer == "Y" || answer == "1" || answer == "yes")
       {return true;}
        if(answer == "n" || answer == "N" || answer == "0" || answer == "no")
       {return false;}
    }
}

void endGame()
{

}
Jack Wetherell
  • 565
  • 3
  • 8
  • 16
  • 1
    How do you print the returned value? `cout << restart()`? – RedX Jul 16 '12 at 16:11
  • Yes. It was used as part of my main loop. A bool value gameRunning was set to it using gameRunning = restart(); and I kept getting 56. So I tested the function itself using cout << restart(); and I still get 56 – Jack Wetherell Jul 16 '12 at 16:14
  • 1
    Does it return 56 in the case of "no" as well? – Chris Dargis Jul 16 '12 at 16:14
  • 1
    Well technically anything not 0 is `true`. I'm not sure if the standard mandates 1. Your code might be able to be simplified to just `std::cout << true;` as well; does that produce the same result? – chris Jul 16 '12 at 16:14
  • 1
    @user1487087: Do you have any strange `typedef`s somewhere? – Ry- Jul 16 '12 at 16:15
  • Yes. In the case of "no" 56 is still the result. – Jack Wetherell Jul 16 '12 at 16:16
  • @user1487087: Does the program start/stop as expected ? Regardless of output? – Chris Dargis Jul 16 '12 at 16:17
  • std::cout produces the same result. – Jack Wetherell Jul 16 '12 at 16:17
  • Ok, that's odd if both `true` and `false` do that. Are you including any strange headers, or have defines or anything? – chris Jul 16 '12 at 16:18
  • 3
    @user1487087: Can you post your entire code, please? (If it's small enough.) And please include included headers and other preprocessor directives. – Ry- Jul 16 '12 at 16:18
  • Doug - The program works with no errors. It is just that answering "no" and "yes" both return 56 and so it restarts the game regardless of what the using inputs here. – Jack Wetherell Jul 16 '12 at 16:19
  • well my code is to make a tick tack toe game and is in early stages so I cannot really post my code. I am making the needed functions one by one and testing them as I go. I will build it all together at the end. This function is all there is when testing and main contails only the cout command. – Jack Wetherell Jul 16 '12 at 16:21
  • @user1487087: If it's in early stages, posting your code shouldn't be a problem... go ahead and just post what you have. It will help. A lot. – Ry- Jul 16 '12 at 16:21
  • my headers are: #include #include – Jack Wetherell Jul 16 '12 at 16:21
  • 1
    @user1487087 Did you reproduce this problem more than once? I [cannot](http://ideone.com/aZhDV). Also note that you might get into an infinite loop... – Eitan T Jul 16 '12 at 16:23
  • 2
    @user1487087, The smallest complete sample that reproduces the problem is what we love. See [sscce.org](http://sscce.org). – chris Jul 16 '12 at 16:24
  • 3
    @user1487087: The code as posted works perfectly fine in gcc4.6 so the problem must be elsewhere. – Grizzly Jul 16 '12 at 16:25
  • 1
    @user1487087: Yes, if you can, please [reproduce the problem on CodePad](http://codepad.org/yuL7MGAS). – Ry- Jul 16 '12 at 16:25
  • entire code added now (sorry for the incompleteness) – Jack Wetherell Jul 16 '12 at 16:27
  • btw functions are all out of main I just indented it wrong when copying it in. – Jack Wetherell Jul 16 '12 at 16:29
  • 1
    Try using `boolalpha( cout );` before you display the value. – Ajay Jul 16 '12 at 16:29
  • 3
    @user1487087: Your complete code works [perfectly fine on gcc4.3](http://ideone.com/BASWY). What compiler are you using? – Grizzly Jul 16 '12 at 16:30
  • codepad: http://codepad.org/LGtEoIlh – Jack Wetherell Jul 16 '12 at 16:31
  • 2
    I don't think this is the problem here, but you have no return value defined outside of the while loop. If somehow you get outside of it, you do not return a value (though I have no idea what behavior is expected or even if any behavior is expected in this case) – ApplePie Jul 16 '12 at 16:32
  • 1
    Yes, all control paths should lead to a return value. – Chris Dargis Jul 16 '12 at 16:33
  • 2
    I have got it working. I added return false; at the end of the function now it works fine. Any Idea how this happens? – Jack Wetherell Jul 16 '12 at 16:33
  • 1
    As is the function cannot exits the while loop, but if it did, OP would get an undefined behavior (warning "not all control paths return a value"). – Jem Jul 16 '12 at 16:33
  • @user1487087 what compiler are you using ? Any difference between "debug" and "release" modes ? – Jem Jul 16 '12 at 16:35
  • Ok. thanks everyone for the help. I'm new to stack overflow. Do you know how I set this so answered? – Jack Wetherell Jul 16 '12 at 16:35
  • 1
    You can post an answer yourself if you found the solution ! – Jem Jul 16 '12 at 16:36
  • 1
    @user1487087: You can accept the correct answer (checkmark underneath its score). In this case (since there is non so far) you can post an answer and accept your one answer. – Grizzly Jul 16 '12 at 16:37
  • I posted my comment as an answer so you can accept it. – ApplePie Jul 16 '12 at 16:37

3 Answers3

4

[This is somewhat of a repost of my comment since OP said it solved his problem]

You have no return value defined outside of the while loop. If somehow you get outside of it, you do not return a value (though I have no idea what behavior is expected or even if any behavior is expected in this case)

To make my answer a bit more thorough, I have found this: Why does flowing off the end of a non-void function without returning a value not produce a compiler error?

Community
  • 1
  • 1
ApplePie
  • 8,814
  • 5
  • 39
  • 60
3

OK, I'm making a guess. Your original code has the line

grid[3][3] = updateGrid(grid, move, playersMove);

And your grid definition is

int grid[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

This means that your are writing out of the array bounds. This is undefined behavior. Please correct this and check if your program works as expected.

PermanentGuest
  • 5,213
  • 2
  • 27
  • 36
1

I used the mingw32 compiler, when using g++ 4.8 it works fine. It seems to be a compiler error. (thanks to grizzly)

Jack Wetherell
  • 565
  • 3
  • 8
  • 16