这段代码中有一些崩溃错误,我应该找到,但我有问题发现它们,我花了很长时间才看到。我相信这很容易,我失踪了。当我运行在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?你认为这个问题的标题会在未来帮助其他人找到并得到这篇文章的帮助吗? –
对不起,我第一次使用这个网站发布了这个内容,这是我在编程中第一堂课。感谢有关如何调试小程序的信息,它帮助了很多。我会确保我的下一篇文章提供了我所需要的更多细节。 –