【问题标题】:C++ Random Number Guessing Game ErrorsC++ 随机数猜测游戏错误
【发布时间】:2017-07-12 20:08:01
【问题描述】:

所以,我必须从一个随机猜谜游戏中编写一个程序。程序需要让玩家猜一个 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;
}

【问题讨论】:

  • int winlose(g) 这不是一个有效的函数声明语法
  • int winlose(g) -> int winlose(int g)
  • [Off Topic] 只调用srand一次,除非你真的知道自己在做什么。
  • 每次猜测后您都在选择一个新数字。这使得数字过高或过低的提示以及他们重试的请求变得毫无意义。
  • x 在首次使用时未初始化。在winlose 中无法访问nreturn n; 永远不会到达,但是winlose 的返回值永远不会被使用。您真的应该花一点时间来理解范围的概念。请参阅此cppreference page 和此Stack Overflow question

标签: c++


【解决方案1】:

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

int winlose(int g);

您不能在 else 语句中使用条件:(else (g=n))。如果没有满足前面的条件(ifelse 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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    相关资源
    最近更新 更多