如何执行我的猜测函数?[关闭]

How do I get my guess function to execute? [closed]

提问人:Ryan Perez 提问时间:11/16/2023 最后编辑:273KRyan Perez 更新时间:11/16/2023 访问量:44

问:


这个问题是由错别字或无法再现的问题引起的。虽然类似的问题可能在这里是主题,但这个问题的解决方式不太可能帮助未来的读者。

6天前关闭。

当我运行我的代码时,guess 函数永远不会运行。playgame 函数运行,但当我输入字母 p 时,guess 函数永远不会运行。我对 C++ 相当陌生,所以我希望能为我面临的这个问题提供一些帮助。

我尝试过调试,但它只是显示该函数从未执行过。

这是我的代码:

    #include<iostream>
#include<cstdlib>
#include <string>
using namespace std;
int playgame();
int getrandnum(int minnum, int maxnum);
int guessing();
int main()
{
    srand(time(0));
    cout << "welcome to the guessing game enter 'q' to quit or 'p' to play\n";
    string input;
    while (true)
    {
        
        if (!playgame())
            break;
    }
    cout << "thanks for playing";
}

int getrandnum(int minnum, int maxnum)
{
    return (rand() % (maxnum - minnum + 1)) + minnum;
}

int guessing()
{
    int maxtries, minnum, maxnum;
    cout << "enter the amount of tries you want: ";
    cin >> maxtries;
    cout << "enter the range of numbers you want\n";
    cout << "minimum number: ";
    cin >> minnum;
    cout << "maximum number: ";
    cin >> maxnum;
    int tries = 0;
    int randnum = getrandnum(minnum, maxnum);
    cout << randnum << endl;
    cout << "welcome you have five guesses\n";
    while (tries < maxtries)
    {
        int guess;
        cin >> guess;
        if (guess < randnum)
        {
            cout << "too low!\n";
        }
        else if (guess > randnum)
        {
            cout << "too high\n";
        }
        else if (guess < minnum || guess > maxnum)
        {
            cout << "the number you entered is not within the given range\n";
            tries--;
        }   
        else if (guess == randnum)
        {
            cout << "you got it!\n";
            break;
        }
        tries++;
    }
    if (tries == maxtries)
    cout << "you ran out of guesses\n";
    cout << "enter 'q' to quit or 'p' to play\n";
    return true;
}


int playgame()
{
    

    string input;
    getline(cin, input);
    if (input == "q")
        return false;
    if (input == "p")
    {
        int guessing();
        return true;
    }
    else
    {
        cout << "that was not a valid input\n";
        cout << "enter 'q' to quit or 'p' to play\n";
        return true;
    }

}
C 可视化 C++

评论

0赞 HolyBlackCat 11/16/2023
int guessing();是一个函数声明。 将是一个函数调用。请注意,在任何其他函数调用之前没有类型。guessing();
0赞 PaulMcKenzie 11/16/2023
当我运行我的代码时,guess 函数永远不会运行——那是因为你从未调用过该函数。出于某种奇怪的原因,您决定这样做:而不是 .guessingint guessing();guessing();
0赞 273K 11/16/2023
不要改变问题的含义!一旦你得到反馈(甚至在评论中得到答案),你就不应该这样做。提出一个新问题。

答: 暂无答案