gpt4 book ai didi

c++ - 未在范围内声明,初学者蜗牛赛车游戏

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:04:47 25 4
gpt4 key购买 nike

好吧,我用谷歌搜索并在这个网站上找到了一些关于这个的答案,但答案差异太大,我无法真正对它们做任何事情,所以我再次询问并希望最好。

我在 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: \n\n"
<< "1) Bet on snail 1\n"
<< "2) Bet on snail 2\n"
<< "3) Bet on snail 3\n"
<< "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>

关于c++ - 未在范围内声明,初学者蜗牛赛车游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7986977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com