2016-09-17 5 views
-5

このコードには、私が見つけるはずのいくつかのクラッシュバグがありますが、私はそれらを見つけるのにかなりの時間を費やしています。私はそれが私が行方不明になっていることは簡単だと確信しています。学校の割り当てに問題がある

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

このコードは次のように私は、Visual Studio 2012でコードを実行すると、私はあなたが

array <Wheel, 3> slotMachine; 

を宣言

#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

あなたのシフトキーは壊れていますか?そして、この質問のタイトルは、他の人が将来、投稿を見つけて助けてくれるのに役立つと思いますか? –

+1

私はこのサイトを初めて使ってこのサイトを投稿したことを申し訳ありません。これはプログラミングで取り上げた最初のクラスです。小さなプログラムをデバッグする方法についての情報をありがとう、それは多くの助けになりました。私は、次の投稿が私が探しているものの詳細を提供していることを確認します。 –

答えて

1

取得し、配列の添字エラーがその後、あなたは、この配列反復処理しましたループのロジックに従って、slotMachine[1]からslotMachine[3]にアクセスします。

残念ながら、slotMachine[3]はありません。そのアクセスには未定義の動作が発生し、クラッシュする可能性があります。

Nの要素の配列またはベクトルには、0からN-1という番号の要素が含まれています。あなたはこの事実を確認するために指を使うことができます。

slotMachine[0]からslotMachine[2]までの配列を含み、slotMachine[1]からslotMachine[3]ではありません。これがC++で配列とベクトルが動作する方法です。

+0

助けてくれてありがとう、私はそれが何か明白であることを知ってくれたが、私はしばらくこの課題の他の部分を行っていた。今後、私はこのサイトに投稿する前に、より慎重に確認していきます。 –

関連する問題