C++猜游戏错误
C++ Guessing Game Error
我不知道如何在"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
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 试图创建一个体面的基本刽子手游戏。猜出的字母不会取代短语中的字母
- 我的游戏引擎的 spdlog 日志记录出现奇怪的"unresolved external symbol"错误
- C++ 中连接 4 游戏的输入逻辑错误
- Visual Studio 中的 lcr 游戏 c++ LINK 2019 错误
- 游戏引擎 SFML C++错误
- C++ do/while 循环和 if/else: 骰子游戏 -- 第 75 行错误:"Cout"之前的预期"While" -- 如何解决这个问题?
- C++随机猜数游戏错误
- 井字游戏中的错误
- 我正在尝试安装游戏引擎,但遇到错误
- 致命错误LNK1169:在C++Visual Studio中编写战舰游戏时发现的一个或多个多重定义的符号
- C++ 抛出读取访问冲突错误,但我不确定为什么。平铺滑块益智游戏
- 如何使用运行时检查错误来修复我的C 格斗游戏
- 我在使用C 的Hangman游戏中遇到错误
- 猜随机数游戏
- 简单的数字猜游戏.c++
- 随机生成器猜游戏
- C++猜游戏错误