【问题标题】:Why this code is executing 2 times the same operation?为什么这段代码执行两次相同的操作?
【发布时间】:2026-02-22 03:35:01
【问题描述】:

所以我有一个基本的猜数字游戏。

在我的int main 中,我有三个函数用来播放它。我有一个围绕这些函数的游戏循环,bool = falsereturn value 设置为等于我的 PlayAgain 函数。

一切运行良好,但当你猜对了数字时,它会询问你是否出于某种原因想再玩两次。

我已尝试删除我在 main 中调用该函数的实例之一:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include<string>


void PrintIntro();
void PlayGame();
bool PlayAgain();

int main() {

    bool bPlayAgain = false;
    do {
        PrintIntro();
        PlayGame();
        PlayAgain();     //I've tried removing this line
        bPlayAgain = PlayAgain(); //I've also played around with this one
    } while (bPlayAgain);

    return (0);
}

void PrintIntro()
{
    std::cout << "Guess a number between 1-100, fool!\n";
}

void PlayGame()
{
    srand(static_cast<unsigned int> (time(0)));
    int HiddenNumber = rand();
    int Number = (HiddenNumber % 100) + 1;
    int Guess;

    do {
        std::cin >> Guess;
        if (Guess > Number) {
            std::cout << "You are too high bro!\n\n";
        }
        else if (Guess < Number) {
            std::cout << "You need to get higher bro!\n\n";
        }
        else if (Guess = Number) {
            std::cout << "You are just high enough, you win!\n\n";
        }
    } while (Guess != Number);
}

bool PlayAgain()
{
    std::string Response = "";
    std::cout << "Would you like to play again? yes or no." << std::endl;
    std::getline(std::cin, Response);
    std::cout << std::endl;

    return (Response[0] == 'y') || (Response[0] == 'Y');
}

【问题讨论】:

  • 欢迎来到 Nicholas 社区。尽管您在询问之前似乎已经对此进行了一些研究,但我鼓励您继续调试您的代码。这很可能是一个简单的错误(调用 PlayAgain 方法的次数超出预期),而 * 确实不是提供此类调试帮助的正确网站。
  • "//I've tried removing this line" 肯定需要删除。
  • 我相信代码PlayAgain(); bPlayAgain = PlayAgain(); 调用了两次函数?如果您尝试删除其中一个,我不明白您的文字。

标签: c++ boolean return-value


【解决方案1】:

这里是固定的代码。我添加了一个名为 Game 的新布尔函数,它玩游戏并在玩家想要重新玩游戏时返回 true。

#include <iostream>
#include <cstdlib>
#include <ctime>
#include<string>


void PrintIntro();
void PlayGame();
bool PlayAgain();
bool Game();

int main() {

    bool bPlayAgain = Game();
    while(bPlayAgain == true)
    {
        bPlayAgain = Game();
    }
    return (0);
}

void PrintIntro()
{
    std::cout << "Guess a number between 1-100, fool!\n";
}

void PlayGame()
{
    srand(static_cast<unsigned int> (time(0)));
    int HiddenNumber = rand();
    int Number = (HiddenNumber % 100) + 1;
    int Guess;

    do {
        std::cin >> Guess;
        if (Guess > Number) {
            std::cout << "You are too high bro!\n\n";
        }
        else if (Guess < Number) {
            std::cout << "You need to get higher bro!\n\n";
        }
        else if (Guess = Number) {
            std::cout << "You are just high enough, you win!\n\n";
        }
    } while (Guess != Number);
}

bool PlayAgain()
{
    char Response;
    std::cout << "Would you like to play again? yes or no." << std::endl;
    std::cin >> Response;
    std::cout << std::endl;

    return (Response == 'y') || (Response == 'Y');
}

bool Game()
{
    PrintIntro();
    PlayGame();
    bool selection = PlayAgain();

    return selection;
}

【讨论】: