這段代碼中有一些崩潰錯誤,我應該找到,但我有問題發現它們,我花了很長時間纔看到。我相信這很容易,我失蹤了。當我運行在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;
解決此類問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –
你的SHIFT鑰匙壞了嗎,Levi?你認爲這個問題的標題會在未來幫助其他人找到並得到這篇文章的幫助嗎? –
對不起,我第一次使用這個網站發佈了這個內容,這是我在編程中第一堂課。感謝有關如何調試小程序的信息,它幫助了很多。我會確保我的下一篇文章提供了我所需要的更多細節。 –