简体   繁体   English

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

[英]C++ Random Number Guessing Game Errors

So, I have to write a program from a random guessing game. 因此,我必须从随机猜谜游戏中编写程序。 The program needs to ask the player to guess a number between 1-100. 该程序需要让玩家猜出一个介于1到100之间的数字。 At least one function must be used. 至少必须使用一种功能。 It needs to tell the player if they are too low/high, ask them to try again, or if they guess it, ask them to play again. 它需要告诉玩家他们是否太低/太高,请他们重试,或者如果他们猜到了,请他们再次玩。

I have a few errors that I can not figure out. 我有一些我无法弄清的错误。

44: error: 'int winlose' redeclared as different kind of symbol 9: error: previous declaration of 'int winlose(int)' 44: error: 'g' was not declared in this scope 44:错误:'int winlose'重新声明为另一种符号9:错误:先前声明了'int winlose(int)'44:错误:在此范围内未声明'g'

Code

#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;
}

You made a few mistakes in addition to the function declaration. 除了函数声明之外,您还犯了一些错误。 Function declarations must contain the type of each parameter, so the correct way is: 函数声明必须包含每个参数的类型,因此正确的方法是:

int winlose(int g);

You cannot use a condition in an else statement: ( else (g=n) ). 您不能在else语句中使用条件:( else (g=n) )。 An else statement is a catch-all, if none of the previous conditions (the ones in the if and else if() 's) were met. 如果之前的条件( ifelse if()的条件)都不满足,则else语句是一个万能的。 If you only want this to trigger on a specific condition, use another else if() . 如果只希望在特定条件下触发,请使用else if() You are not required to have an else at the end of every if statement; 您无需在每个if语句的末尾添加else it is perfectly correct to end with an else if(){...} . else if(){...}结尾是完全正确的。

You also need to compare with '==' rather than '=' . 您还需要与'=='而不是'=' = is the assignment operator and g=n will set the value of g to n. =是赋值运算符,并且g=n会将g的值设置为n。 If you want to check if g is equal to n, you will have to use g==n 如果要检查g是否等于n,则必须使用g==n

You should call srand() in the outer loop, otherwise after every guess, value changes. 您应该在外部循环中调用srand() ,否则在每次猜测之后,值都会改变。

The rest is corrected and sometimes slightly changed for correct performance: 其余部分已得到纠正,有时会稍作更改以实现正确的性能:

#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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM