gpt4 book ai didi

C++ bool 函数返回 56

转载 作者:可可西里 更新时间:2023-11-01 18:29:40 29 4
gpt4 key购买 nike

我有一个返回 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()
{

}

最佳答案

[这在某种程度上是我评论的转贴,因为 OP 说它解决了他的问题]

您没有在 while 循环外定义返回值。如果您以某种方式超出它,则不会返回值(尽管我不知道预期的行为是什么,甚至在这种情况下是否预期有任何行为)

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

关于C++ bool 函数返回 56,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11508228/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com