2016-09-17 57 views
-5

這段代碼中有一些崩潰錯誤,我應該找到,但我有問題發現它們,我花了很長時間纔看到。我相信這很容易,我失蹤了。當我運行在Visual Studio 2012的代碼中,我得到與數組下標錯誤在學校任務中​​遇到問題

#include <iostream> // provides access to cin and cout 
#include <iomanip> 
#include <array> 

#include <vector> 


using namespace std; 

int main() 
{ 
    // seed random number generator 
    srand(time(NULL)); 

enum symbol 
{ 
    Lemon, Cherry, Orange, Bell, Jackpot 
}; 
// create a struct for slot machine wheel 
struct Wheel 
{ 
    array <string, 10> symbols; 
    array <symbol, 10> eSymbols; 
    int position; 
    string selected; 
}; 
//create an array of three slot machine wheels 
array <Wheel, 3> slotMachine = 
{ 
    { 
     { 
      {"Cherry", "Orange", "Lemon", "Orange", "Bell", "Orange", "Lemon", "Cherry", "Jackpot", "Bell"}, 
      {Cherry, Orange, Lemon, Orange, Bell, Orange, Lemon,Cherry, Jackpot, Bell}, 
      0,"Cherry" 
     }, 
     { 
      {"Cherry", "Bell", "Lemon", "Orange", "Bell", "Jackpot", "Lemon", "Cherry", "Jackpot", "Bell"}, 
      {Cherry, Bell, Lemon, Orange, Bell, Jackpot, Lemon, Cherry, Jackpot, Bell}, 
      1,"Bell" 
      }, 
      { 
       {"Cherry", "Orange", "Lemon", "Orange", "Lemon", "Orange", "Lemon","Cherry", "Jackpot", "Bell"}, 
       {Cherry, Orange, Lemon, Orange, Lemon, Orange, Lemon, Cherry, Jackpot, Bell}, 
       2,"Lemon" 
      } 
    } 
}; 

bool gameOn = true; 
bool winner = false; 
int thePot = 100; 
int bet = 1; 
vector <int> combo; 
while (gameOn) 
{ 
    for (int i = 1; i < 4; i++) 
    { 
     slotMachine[i].position =(slotMachine[i].position + rand()%10)%10; 
     slotMachine[i].selected = slotMachine[i].symbols[slotMachine[i].position]; 
     cout << setw(10) << left << slotMachine[i].selected.c_str() ; 
     combo.push_back(slotMachine[i].eSymbols[slotMachine[i].position]); 
    } 
    if ((combo[0] == combo[1]) && (combo[1] == combo[2])) 
    { 
     if (combo[0] == Lemon) 
     { 
      cout << "You keep your bet." << endl; 
     } 
     else if(combo[0] = Jackpot) 
     { 
      cout << "**** You hit $1000 Jackpot!!! ****" << endl; 
      thePot += 1000; 
      winner = true; 
      gameOn = false; 
     } 
     else 
     { 
      cout << "WINNER! You win $" << combo[0]*5 << endl; 
      thePot += combo[0]*5; 
     } 
    } 
    else 
    { 
     thePot -= bet; 
     if (thePot > 0) gameOn=false; 
    } 
    cout << "You now have $" << thePot << endl; 
    combo.clear(); 
    cout << endl; 
    cin.get(); 
} 
if (winner) cout << "You walk away a winner." << endl; 
else cout << "You have lost all your money." << endl; 
// Wait for user input to close program when debugging. 
cin.get(); 
return 0; 
+0

解決此類問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –

+0

你的SHIFT鑰匙壞了嗎,Levi?你認爲這個問題的標題會在未來幫助其他人找到並得到這篇文章的幫助嗎? –

+1

對不起,我第一次使用這個網站發佈了這個內容,這是我在編程中第一堂課。感謝有關如何調試小程序的信息,它幫助了很多。我會確保我的下一篇文章提供了我所需要的更多細節。 –

回答

1

你宣佈一個

array <Wheel, 3> slotMachine; 

之後,你通過這個數組反覆如下:

for (int i = 1; i < 4; i++) 
{ 
    slotMachine[i].position = 

此代碼根據循環的邏輯,將訪問slotMachine[1]slotMachine[3]

不幸的是,沒有slotMachine[3],試圖訪問它會導致未定義的行爲,並可能導致崩潰。

N元素的數組或向量包含編號從0到N-1的元素。你可以用你的手指來驗證這個事實。

此數組包含slotMachine[0]slotMachine[2],而不是slotMachine[1]slotMachine[3]。這就是數組和向量在C++中的工作原理。

+0

謝謝你的幫助,我知道這是明顯的東西,但我一直在盯着屏幕做了一段時間做這個任務的其他部分。在將來發布到本網站之前,我將確保仔細檢查。 –