2016-09-17 78 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

谢谢你的帮助,我知道这是明显的东西,但我一直在盯着屏幕做了一段时间做这个任务的其他部分。在将来发布到本网站之前,我将确保仔细检查。 –