Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Boolean function returning 56

Tags:

c++

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()
{

}
like image 772
Jack Wetherell Avatar asked Jul 16 '12 16:07

Jack Wetherell


2 Answers

[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?

like image 58
ApplePie Avatar answered Sep 30 '22 00:09

ApplePie


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.

like image 26
PermanentGuest Avatar answered Sep 30 '22 00:09

PermanentGuest