我不知道如何在“int main()”的括号中声明“random”,需要帮助。 (我是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();
}
这是更新的编译器错误:
|| === Build:Guess中的调试编号(编译器:GNU GCC编译器)=== |
C:\ Users \ Minecraftship \ Documents \ CPP程序来自Book \ Guess Number \ main.cpp || 在函数'int main()'中:|
C:\ Users \ Minecraftship \ Documents \ CPP程序来自Book \ Guess Number \ main.cpp | 12 |
错误:未在此范围中声明'randomize'|
|| ===构建失败:1个错误,0个警告(0分钟,0秒(秒))=== |
答案 0 :(得分:10)
删除main附近的分号,编译器正在告诉你究竟是什么问题:
int main ();
应该是
int main ()
您的代码在修复此代码后也无法编译,因为您尚未声明std命名空间。您现在可以将此行放在顶部using namespace std;
,但它是bad practice。您应该使用范围解析运算符手动声明它。
上面的评论中已经提到的许多其他问题,请确保彻底阅读编译器输出,因为它会告诉您导致问题的行。
您的代码应如下所示:
#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();
}
答案 1 :(得分:2)
其他人得到了它。签名后,像main()这样的方法没有分号。
另一件未提及的事情,我猜你想要
while (u != rn)
另外,请注意&#34; =&#34;和&#34; ==&#34;。
BTW - 欢迎使用C ++ !!!
答案 2 :(得分:0)
更便携的版本(不使用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!";
}