【问题标题】:C++ Guessing Game ErrorC++ 猜谜游戏错误
【发布时间】:2016-07-15 02:50:23
【问题描述】:

我不知道如何在“int main()”的括号中声明“随机”,需要帮助。 (我是 C++ 初学者)

请查看我的代码,试一试,当您认为您知道如何解决此问题时,请通知我并提供答案。这对我来说意义重大。谢谢!同时,我也会继续努力自己解决问题。

注意:如果你想具体一点,我正在使用 Code::Blocks。

错误出现在我的代码的第 7/9 行

下面是我的更新代码:

#include <iostream>
#include <stdlib.h>
#include <conio.h>

using namespace std;

int main()
{
int rn = random() % 21; // generates a random int from 0 to 20

// First output asking the user to guess the number
cout << "Please guess my number :" << endl;
int u;
cin >> u;

while (u != rn) // Calculates the answer that you give
{

// If the user's number is greater than the random number
// the program will let you know it's too large
if (u > rn)
{
    cout << "You guessed too big!" << endl;
}
// On the other hand, if the user guesses to small
// the program will tell them that it's too small
else if (u < rn)
{
    cout << "You guessed too small!" << endl;
}

// If the user does not get the right number, the program
// will tell the user to guess again
cout << "Please guess again :" << endl;
cin >> u;

}

// If the user guesses the number correctly, the program
// will say that they got it right, and end the program
cout << "You guessed it right!" << endl;
getch();
}

这是更新后的编译器错误:

||=== 构建:在猜数字中调试(编译器:GNU GCC 编译器)===|

C:\Users\Minecraftship\Documents\CPP Programs From Book\Guess The Number\main.cpp||In function 'int main()':|

C:\Users\Minecraftship\Documents\CPP Programs From Book\Guess The Number\main.cpp|12|

错误:“随机化”未在此范围内声明|

||=== 构建失败:1 个错误,0 个警告(0 分钟,0 秒)===|

【问题讨论】:

  • 您的编译器是否告诉您错误在哪一行?如果是这样,这些信息会很有帮助。
  • 第 6 行有一个分号不属于:int main();
  • 这个while (u = rn) 看起来很可疑
  • 我认为@yussuf 是正确的。那里不应该有分号。
  • 请注意,我刚刚删除了您 90% 的叙述,实际上使您的问题更清晰、更易于阅读。

标签: c++ compiler-errors


【解决方案1】:

去掉 main 附近的分号,编译器会告诉你问题出在哪里:

int main ();

应该是

int main ()

即使在修复此问题后,您的代码也不会编译,因为您尚未声明 std 命名空间。您现在可以将此行放在顶部using namespace std;,但它是bad practice。您应该使用范围解析运算符手动声明它。

以及上面 cmets 中已经提到的许多其他问题,请务必仔细阅读编译器输出,因为它会告诉您是哪一行导致了问题。

您的代码应如下所示:

#include <iostream>
#include <stdlib.h>
#include <conio.h>

using namespace std;

int main()
{
int rn = random() % 21; // generates a random int from 0 to 20

// First output asking the user to guess the number
cout << "Please guess my number :" << endl;
int u;
cin >> u;

while (u != rn) // Calculates the answer that you give
{

    // If the user's number is greater than the random number
    // the program will let you know it's too large
    if (u > rn)
    {
        cout << "You guessed too big!" << endl;
    }
    // On the other hand, if the user guesses to small
    // the program will tell them that it's too small
    else if (u < rn)
    {
        cout << "You guessed too small!" << endl;
    }

    // If the user does not get the right number, the program
    // will tell the user to guess again
    cout << "Please guess again :" << endl;
    cin >> u;

}

// If the user guesses the number correctly, the program
// will say that they got it right, and end the program
cout << "You guessed it right!" << endl;
getch();
}

【讨论】:

  • 我已经添加了 using namespace std;,并尝试了许多其他修复,但由于某种原因错误仍然存​​在......
  • 呃。我还是这个地方的新手,它说我不能在另一天发布另一个问题......有什么建议吗?
  • 抱歉让您久等了,我一直很忙。是的,我刚刚粘贴了将代码编译到问题中的错误。它仍然不想工作。
  • 代码现已更新。我刚刚删除了一堆不需要的分号,然后放了一个“!”在“=”之前标记:while (u != rn)。
  • 我意识到我还添加了:using namespace std;.
【解决方案2】:

其他人得到了它。 main() 等方法的签名后没有分号。

还有一件事没有提到,我猜你想要

while (u != rn)

另外,请注意“=”和“==”的区别。

顺便说一句——欢迎使用 C++!!!

【讨论】:

  • 非常感谢,这对 !标记,但错误仍然存​​在。
  • 哦,由于某种原因,最后一部分没有在我的电脑屏幕上注册。感谢您的欢迎布赖恩! :)
【解决方案3】:

一个更便携的版本(不使用 conio.h),它可以让计算机与自己对战:

#include <iostream>
#include <cstdlib>
#include <ctime>

int get_random_in_range(int min, int max)
{
    return std::rand() % (max - min) + min;
}

// returns 0 if user guessed right, negative value if user
// guessed too small, positive if user guessed too big
int check_user_guess(int guess, int my_secret)
{
    return guess - my_secret;
}

int main ()
{
    int my_guess = get_random_in_range(1, 10);

    std::cout << "I think of " << my_guess << std::endl;

    std::cout << "Please guess my number: ";
    int user_guess = get_random_in_range(1, 10);
    std::cout << user_guess << std::endl;

    while (check_user_guess(user_guess, my_guess) != 0)
    {
        std::cout << "You guessed " << user_guess << std::endl;

        if (check_user_guess(user_guess, my_guess) > 0)
        {
            std::cout << "You guessed too big!" << std::endl;
        }
        else if (check_user_guess(user_guess, my_guess) < 0)
        {
            std::cout << "You guessed too small!" << std::endl;
        }

        std::cout << "Please guess again: ";
        user_guess = get_random_in_range(1, 10);
        std::cout << user_guess << std::endl;

    }

    std::cout << std::endl << "You guessed it right!";
}

在这里试试:http://coliru.stacked-crooked.com/a/5bf0b9201ef57529

【讨论】:

  • 谢谢优素福!我很感激。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多