提问人:Ryan Perez 提问时间:11/16/2023 最后编辑:273KRyan Perez 更新时间:11/16/2023 访问量:44
如何执行我的猜测函数?[关闭]
How do I get my guess function to execute? [closed]
问:
当我运行我的代码时,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;
}
}
答: 暂无答案
评论
int guessing();
是一个函数声明。 将是一个函数调用。请注意,在任何其他函数调用之前没有类型。guessing();
guessing
int guessing();
guessing();