C++猜游戏错误

C++ Guessing Game Error

本文关键字:错误 猜游戏 C++      更新时间:2023-10-16

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

看一下我的代码,尝试一下,当你认为你知道如何解决这个问题时,请通知我一个答案。这对我来说意义重大。谢谢!同时,我也将继续尝试自己解决问题。

注意:如果你想具体一点,我正在使用代码::块。

错误在我的代码的第 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:\用户\我的世界\文档\CPP程序从书\猜数字\主.cpp||在函数 'int main()' 中:|

C:\用户\我的世界\文档\来自书的 CPP 程序\猜数字\主.cpp|12|

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

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

删除main附近的分号,编译器会准确地告诉您问题所在:

int main ();

应该是

int main ()

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

以及上面评论中已经提到的许多其他问题,请确保仔细阅读编译器输出,因为它告诉您导致问题的行。

您的代码应如下所示:

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

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

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

while (u != rn)

另外,请注意"="和"=="中的区别。

顺便说一句 -- 欢迎来到C++!!!

一个

更便携的版本(不使用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