C ++布尔函数返回56

时间:2012-07-16 16:09:38

标签: c++

我有一个返回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;}
    }
}

当我使用以下方式调用它时:

cout << restart();

我得到了输出:

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

有谁能看到如何解决这个奇怪的问题? 提前谢谢。

我的WIP代码现在是:

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

}

3 个答案:

答案 0 :(得分:4)

[这是我的评论的一个转贴,因为OP说它解决了他的问题]

您没有在while循环之外定义的返回值。如果你以某种方式超出它,你就不会返回一个值(虽然我不知道预期的行为是什么,或者即使在这种情况下预期会有任何行为)

为了让我的答案更加彻底,我发现了这个: Why does flowing off the end of a non-void function without returning a value not produce a compiler error?

答案 1 :(得分:3)

好的,我猜了。您的原始代码有

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

您的网格定义是

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

这意味着你正在写出数组边界。这是未定义的行为。 请更正此问题,并检查您的程序是否按预期工作。

答案 2 :(得分:1)

我使用mingw32编译器,使用g ++ 4.8时工作正常。这似乎是一个编译器错误。 (感谢灰熊)