不在申报范围内,初学者蜗牛竞速游戏
not declared in scope, beginners snail racing game
好吧,我在谷歌上找到了一些关于这个问题的答案,但是答案太不同了,我无法真正做任何事情,所以我再问一遍,希望能有最好的结果。
我在g++中得到了以下错误;
snailrace.cpp: In function ‘int race(int, int)’:
snailrace.cpp:101:21: error: ‘rand’ was not declared in this scope
snailrace.cpp:123:3: error: a function-definition is not allowed here before ‘{’ token
snailrace.cpp:128:3: error: expected ‘}’ at end of input
我想我将能够解决除了snailrace.cpp:101:21: error: ‘rand’ was not declared in this scope
的一切,我尝试了一切,它没有帮助!我怎么解决这个问题!?我都快疯了。谢谢!
//The snail race.
#include <iostream>
#include <ctime>
//Function declaration.
int race(int,int);
void race(void);
int menu(void);
int placebet(int);
void init(void);
//Variables
int money = 200;
//the main function
int main(void)
{
using std::cout;
init();
int user_respons;
cout << "Welcome to the snail race!n";
while(user_respons = menu())
{
switch(user_respons)
{
case 1:
case 2:
case 3:
::money +=
race(placebet(user_respons), user_respons);
break;
case 4: //The user did not bet
race();
break;
}
}
return 0;
}
//Display user menu and sends user selection to the main function
int menu( void )
{
using std::cout;
using std::cin;
int user_respons;
cout << "You have " << money << " USD.n";
do
{
cout << "Race Menu: nn"
<< "1) Bet on snail 1n"
<< "2) Bet on snail 2n"
<< "3) Bet on snail 3n"
<< "4) Do not bet, just watching"
<< "0) Leave the race";
cin >> user_respons;
}
while(user_respons < 0 && user_respons > 4);
return user_respons;
}
//Decide how much one person will bet on a snail.
int placebet(int user_respons)
{
using std::cout;
using std::cin;
int bet_amount;
cout << "Snail " << user_respons << " is a good choice!n";
cout << "How much would you like to bet on your snail?n";
cin >> bet_amount;
return bet_amount;
}
//if just watching the race
void race(void)
{
race(0, 0);
}
//if they are betting money.
int race(int money, int user_respons)
{
using std::cout;
using std::cin;
//Stores random number
int winner = rand() % 3 + 1;
cout << "And the snails are off!n";
cout << "Look at them go! n";
cout << "The winner snail is " << winner << "n";
if(winner == user_respons)
{
cout << "You win!n";
return money * 2;
}
else
{
cout << "You lost.n";
return money / 2;
}
//Handles the program start random
void init(void)
{
using std::srand;
using std::time;
srand(time(0));
}
rand
是您未包含的cstdlib
的一部分。添加
#include <cstdlib>
您在void init(void)
前面忘记加大括号了
int race(int money, int user_respons)
{
/* ... */
else
{
cout << "You lost.n";
return money / 2;
}
} /* <---- missing */
//Handles the program start random
void init(void)
{
using std::srand;
using std::time;
srand(time(0));
}
这可能是由于不一致的缩进。
对于第一个错误,您需要#include <stdlib.h>
(rand需要)
如果你使用gcc编码,那么你必须包含
#include<cstdlib>
相关文章:
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 在猜谜游戏 c++ 中不计算尝试次数
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 在为我的基于文本的 RPG 游戏制作库存时遇到困难
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- Frank Luna 在他的书"使用 DirectX12 进行 3D 游戏编程"的介绍中盒子示例的问题
- 如何在Linux上正确发布C++软件(游戏)
- 提高基于组件的游戏引擎的效率
- 不在申报范围内,初学者蜗牛竞速游戏