2011-11-02 72 views
1

好吧,我用Google搜索,並在這裏找到一些答案在本網站關於這一點,但答案是太不一樣了,我真的它們做任何事情,所以我再問一次,希望爲最好。範圍沒有宣佈,初學者蝸牛競速遊戲

我用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)); 
    } 
+2

這是功課編碼?如果是這樣,添加一個標籤.. –

+0

@ R.MartinhoFernandes不錯鹽 – Jared

+2

蝸牛賽跑聽起來不可思議 –

回答

17

randcstdlib一部分,你不包括。添加

#include <cstdlib> 
+0

謝謝! #include 是問題所在,你看我知道了,但我仍然錯過了它。 –

+0

沒問題,你的蝸牛比賽運氣不錯,也許考慮隨機爲真正的蝸牛比賽準備線程! – Joe

+0

呵呵,我總覺得'rand()'出於某種原因在''裏。 Google說:「rand ctime」:3,810,000個結果,「rand cstdlib」:91,200個結果。 –

3

您忘記了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)); 
} 

這可能是由於縮進不一致造成的。

2

對於第一個錯誤,你需要#include <stdlib.h>(必須爲rand

+0

編輯...沒什麼。 –

2

如果你正在使用gcc,那麼你必須包括

#include<cstdlib>