【发布时间】: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