2013-08-27 67 views
0

我已經成功編譯/運行了其他程序,但是當我嘗試編譯/運行下面的代碼時,它告訴我:「這個項目還沒有建好,你現在想建立它嗎? 。在CodeBlocks中編譯程序

當我從函數getWinnings()中取出int b時,它會編譯/運行,但如果我添加更多參數code :: blocks給我上面的消息。這是什麼造成的?

#include <iostream> 
    #include <ctime> 
    #include <cstdlib> 
    #include <string> 

    int gameCoins; 

    using namespace std; 

    void instructions(); 
    void playSlots(); 
    void spin(); 

    int main() 
    { 
    srand(time(NULL)); 
    instructions(); 
    playSlots(); 
    } 

    void instructions() 
    { 
    cout << "\t\t" << "VIRTUAL SLOT MACHINE" << "\n\n"; 
    cout << "INSTRUCTIONS: " << endl << "You start with 500 coins. The game is over either when you run out of coins or you quit the game." << "\n"; 
    cout << "It will cost you 25 coins per spin. Good luck!" << "\n\n" ; 
    } 
    void playSlots() 
    { 
    string user_input; 
    string replay_or_quit; 

    while (replay_or_quit != "n" && replay_or_quit != "no") 
    { 
    gameCoins = 500; 
    cout << "Coins: " << gameCoins << "\n"; 
    while (user_input != "spin" && user_input != "Spin") 
    { 
     cout << "Spin the wheels to start playing. Type 'spin' to begin" << "\n"; 
     cin >> user_input; cout << "\n\n"; 
    } 
    spin(); 
    while (gameCoins > 0 && user_input != "no" && user_input != "n") 
    { 
     cout << "Spin again?" << "\n" << "Decision: "; 
     cin >> user_input; 
     if (user_input == "y" || user_input == "yes") 
     spin(); 
    } 
    if (gameCoins == 0) 
    { 
     cout << "You went broke. Play again?" << "\n" << "Decision: "; 
     cin >> replay_or_quit; 
    } 
    else 
    { 
     cout << "Congratulations, you won " << gameCoins << " coins. Play again?" << "\n" << "Decision: "; 
     cin >> replay_or_quit; 
    } 
} 
} 

    int getWinnings(int a, int b) 
    { 
    return (a+b); 
    } 
    void spin() 
    { 
int wheelOne_Top, wheelOne_Mid, wheelOne_Bot; 
int wheelTwo_Top, wheelTwo_Mid, wheelTwo_Bot; 
int wheelThree_Top, wheelThree_Mid, wheelThree_Bot; 
int coinsWon; 

gameCoins = gameCoins-25; 
wheelOne_Top = (rand() % 3) + 1, wheelTwo_Top = (rand() % 3) + 1, wheelThree_Top = (rand() % 3) + 1; 
wheelOne_Mid = (rand() % 3) + 1, wheelTwo_Mid = (rand() % 3) + 1, wheelThree_Mid = (rand() % 3) + 1; 
wheelOne_Bot = (rand() % 3) + 1, wheelTwo_Bot = (rand() % 3) + 1, wheelThree_Bot = (rand() % 3) + 1; 

while (wheelOne_Top == wheelOne_Mid || wheelOne_Top == wheelOne_Bot || wheelOne_Bot == wheelOne_Mid) 
{ 
    wheelOne_Mid = (rand() % 3) + 1; 
    wheelOne_Bot = (rand() % 3) + 1; 
} 
while (wheelTwo_Top == wheelTwo_Mid || wheelTwo_Top == wheelTwo_Bot || wheelTwo_Bot == wheelTwo_Mid) 
{ 
    wheelTwo_Mid = (rand() % 3) + 1; 
    wheelTwo_Bot = (rand() % 3) + 1; 
} 
while (wheelThree_Top == wheelThree_Mid || wheelThree_Top == wheelThree_Bot || wheelThree_Bot == wheelThree_Mid) 
{ 
    wheelThree_Mid = (rand() % 3) + 1; 
    wheelThree_Bot = (rand() % 3) + 1; 
} 

cout << "Coins: " << gameCoins << endl << endl; 
cout << "\t\t\t" << wheelOne_Top << " " << wheelTwo_Top << " " << wheelThree_Top << "\n"; 
cout << "\t\t\t" << wheelOne_Mid << " " << wheelTwo_Mid << " " << wheelThree_Mid << "\n"; 
cout << "\t\t\t" << wheelOne_Bot << " " << wheelTwo_Bot << " " << wheelThree_Bot << "\n\n"; 

coinsWon = getWinnings(wheelOne_Top, wheelOne_Mid); 
gameCoins = coinsWon + gameCoins; 

if (gameCoins > 0) 
    cout << "You won " << gameCoins << " coins!" << "\n" << "Total Coins: " << gameCoins << "\n\n"; 
else 
    cout << "You lost 25 coins!" << "\n" << "Total coins: " << gameCoins << "\n\n"; 
} 
+0

當它說「這個項目還沒有建成,你想現在建立它?」你必須點擊「是」。 – 0x499602D2

+0

我點擊是和沒有。沒有錯誤,沒有程序,只是在構建日誌中。就像我說的,它會編譯如果我從函數getWinnings()中刪除int b,但是一旦我添加更多的參數,我什麼也得不到。 --------------編譯:在老虎機中調試(編譯器:GNU GCC編譯器)--------------- mingw32-g ++ .exe - o「bin \ Debug \ Slot Machine.exe」obj \ Debug \ main.o 進程以狀態0終止(0分鐘,1秒) 0錯誤,0警告(0分鐘,1秒) –

+0

然後您必須運行該方案,打F9來做到這一點。 – 0x499602D2

回答

0

確保程序的最後一個實例沒有運行。檢查你的任務管理器,並強制關閉Slot Machine.exe,如果你看到它。

0

它可能是編譯器的行爲有趣,或者它可能是關於環境的東西。

編譯器:

如果它只是導致它崩潰(這可能會保持代碼::塊從打開可執行文件,並讓它給你的消息),編譯一些奇怪的故障,那麼我建議改變...

int getWinnings (int a,int b) 
{ 
    return a + b; 
}; 

...到#define getWinnings(a,b) (a + b)

環境:[我假設你正在使用Windows]

我有時會在兩種情況下得到該通知。

第一個是我無意中碰到Build &在我正在修復的頭文件上運行,而不是在我使用測試頭的實際程序上運行。在這種情況下,如果您給源文件錯誤的擴展名關閉它,請將擴展名更改爲「.c」或「.cpp」而不是「.h」,然後重新打開。

第二個是Code :: Blocks在編寫程序關閉文件之後,在Windows打開它以讀取訪問權之前查找可執行文件。在這種情況下,在構建它之後,如果它給出了消息,只需點擊「否」,然後點擊運行按鈕(而不是Build &運行按鈕)。

如果這些都不是問題,那麼您可能遇到了文件訪問問題,例如您不在管理員級別的帳戶中,或者Code :: Blocks尚未被授予管理員級別的權限。嘗試設置Code :: Blocks以管理員級別權限打開(在Windows 7上,這意味着您必須單擊彈出窗口才能在每次打開時允許它),或者如果您不能這樣做,那麼也許改變你正在編譯的目錄。