Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;随机数猜测游戏错误_C++ - Fatal编程技术网

C++ C++;随机数猜测游戏错误

C++ C++;随机数猜测游戏错误,c++,C++,所以,我必须从一个随机猜测游戏中编写一个程序。程序需要让玩家猜一个介于1-100之间的数字。必须至少使用一个函数。它需要告诉玩家他们是否太低/太高,请他们再试一次,或者如果他们猜到了,请他们再试一次 我有几个错误我想不出来 44:错误:“int winlose”重新声明为不同类型的符号 9:错误:“int winlose(int)”的上一个声明 44:错误:“g”未在此范围内声明 代码 #include <iostream> #include <cstdlib> #inc

所以,我必须从一个随机猜测游戏中编写一个程序。程序需要让玩家猜一个介于1-100之间的数字。必须至少使用一个函数。它需要告诉玩家他们是否太低/太高,请他们再试一次,或者如果他们猜到了,请他们再试一次

我有几个错误我想不出来

44:错误:“int winlose”重新声明为不同类型的符号 9:错误:“int winlose(int)”的上一个声明 44:错误:“g”未在此范围内声明

代码

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int winlose(int);

int main()
{
    int g, n, x;

    while (x!=0)
    {
        do
        {
            srand(time(NULL));
            n = 1 + rand()%100;

            cout<<"Welcome to the guessing game. I have a number between 1-100. 
               Can you guess it?"<<endl;
            cout<<"Please enter your guess"<<endl;
            cin>>g;

        winlose(g); 
        } while (n!=0);

        cout<<"Play again? Enter 0 for no, any other number for yes."<<endl;
        cin>>x;
    }
    return 0;
}

int winlose(g)
{
    if (g<n)
    {
        cout<<"Your guess is too low. Try again."<<endl;
        cin>>g;
    }
    else if (g>n)
    {
        cout<<"Your guess is too high. Try again."<<endl;
        cin>>g;
    }
    else (g=n)
    {
        cout<<"Congrats! You win. The number was "<<n<<endl;
        n=0;
    }
    return g;
    return n;
}
#包括
#包括
#包括
使用名称空间std;
int-winlose(int);
int main()
{
int g,n,x;
而(x!=0)
{
做
{
srand(时间(空));
n=1+rand()%100;

除了函数声明之外,您还犯了一些错误。 函数声明必须包含每个参数的类型,因此正确的方法是:

int winlose(int g);
不能在else语句中使用条件:(
else(g=n)
)。如果前面的条件(如
if
else if()
中的条件)均未满足,则else语句是一个包罗万象的语句。如果您只想在特定条件下触发此条件,请使用另一个
else if()
。不要求在每个
if
语句的末尾都有
else
;以
else if(){…}
结尾是完全正确的

您还需要与
'=='
进行比较,而不是
'='
=
是赋值运算符,
g=n
将g的值设置为n。如果要检查g是否等于n,则必须使用
g==n

您应该在外部循环中调用
srand()
,否则每次猜测后,值都会更改

其余部分已更正,有时会稍微更改以获得正确的性能:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

bool winlose(int number, int givenNumber);

int main(){
    int g, n, x;
    bool guessed;
    do {
        srand(time(NULL));
        n = 1 + rand()%100;
        cout<<"Welcome to the guessing game. I have a number between 1-100. Can you guess it?"<<endl;
        do {
            cout<<"Please enter your guess"<<endl;
            cin>>g;
            guessed = winlose(g, n); 
        } while (!guessed);

        cout<<"Play again? Enter 0 for no, any other number for yes."<<endl;
        cin>>x;
    } while (x!=0);
    return 0;
}

bool winlose(int g, int n) {
    if (g<n) {
        cout<<"Your guess is too low. Try again."<<endl;
        return false;
    }
    else if (g>n) {
        cout<<"Your guess is too high. Try again."<<endl;
        return false;
    }
    else {
        cout<<"Congrats! You win. The number was "<<n<<endl;
        return true;
    }
}
#包括
#包括
#包括
使用名称空间std;
布尔winlose(整数,整数给定数);
int main(){
int g,n,x;
布尔猜测;
做{
srand(时间(空));
n=1+rand()%100;

cout
int-winlose(g)
这不是有效的函数声明语法
int-winlose(g)
->
int-winlose(int-g)
[主题外]除非你真的知道自己在做什么,否则只能调用
srand
一次。每次猜测后你都会选择一个新号码。这意味着该号码太高或太低,以及他们重试的请求都毫无意义。
x
在第一次使用时未初始化。
n
winlose
中无法访问e> return n;
将永远不会被访问,但是
winlose
的返回值永远不会被使用。您应该花一分钟的时间来理解作用域的概念。请参见此和此。