2016-10-08 5 views
-2

これは私のコード全体です。問題は、複数のチップを落として、チップ当たり平均勝利額をプリントアウトしようとすると、0になるということです。私は本当に固執しています。私はそれが簡単に修正できると確信している、多分私は何かを欠場しているか、何か間違っていた。しかし私は私の人生はそれを修正する方法を理解することはできません。助けてください!ありがとうございました!!!!!!C++のループ(plink0)で変数を合計しようとしました

(と私は機能を使用することが容易になるだろう知っているが、この割り当てに我々は関数を使用しないように言われている)

#include <iostream> 
#include <iomanip> 
#include <cstdlib> 

using namespace std; 

int main() 
{ 
    //input variables 

    int menu_selection = 0; 
    double slot_selection = 0; 
    int number_of_chips_drop = 0; 

    //output variables 
    double path_single_chip = 0; 
    double reward_single = 0; 
    double reward_total = 0; 
    double reward_average = 0; 

    //constant variables 
    const double REWARD_0 = 100.0; 
    const double REWARD_1 = 500.0; 
    const double REWARD_2 = 1000.0; 
    const double REWARD_3 = 0.0; 
    const double REWARD_4 = 10000.0; 
    const double REWARD_5 = 0.0; 
    const double REWARD_6 = 1000.0; 
    const double REWARD_7 = 500.0; 
    const double REWARD_8 = 100.0; 

    //computation variables 
    double position_chip = 0; 
    double winning = 0; 


    for (;;) 
    { 
     //menu display 
     cout << "Welcome to the Plinko Simulator!"; 
     cout << endl << endl; 
     cout << "MENU: Please select one of the following options: "; 
     cout << endl << endl; 
     cout << "0 - Quit the program"; 
     cout << endl << endl; 
     cout << "1 - Drop a single chip into one slot"; 
     cout << endl << endl; 
     cout << "2 - Drop multiple chips into one slot"; 
     cout << endl << endl; 
     cout << "Enter your selection now: "; 
     cin >> menu_selection; 
     cout << endl; 

     while (menu_selection != 1 && menu_selection != 2 && menu_selection != 0) // error check menu 
     { 
      cout << "INVALID SELECTION Please enter 0, 1, or 2"; 
      cout << endl << endl; 
      cout << "MENU: Please select one of the following options: "; 
      cout << endl << endl; 
      cout << "0 - Quit the program"; 
      cout << endl << endl; 
      cout << "1 - Drop a single chip into one slot"; 
      cout << endl << endl; 
      cout << "2 - Drop multiple chips into one slot"; 
      cout << endl << endl; 
      cout << "Enter your selection now: "; 
      cin >> menu_selection; 
      cout << endl; 
     } 

     if (menu_selection == 0) 
     { 
      return 0; 
     } 

     if (menu_selection == 1) //simulate one chip drop 
     { 
      cout << "*** DROP SINGLE CHIP ***"; 
      cout << endl << endl; 
      cout << "Which slot do you want to drop the chip in (0-8)? "; // this will be starting point 
      cin >> slot_selection; 
      cout << endl; 

      if (slot_selection >= 9 || slot_selection <= -1) // error check slot value 
      { 
       cout << "INVALID SLOT."; 
       cout << endl << endl; 
       cout << "MENU: Please select one of the following options: "; 
       cout << endl << endl; 
       cout << "0 - Quit the program"; 
       cout << endl << endl; 
       cout << "1 - Drop a single chip into one slot"; 
       cout << endl << endl; 
       cout << "2 - Drop multiple chips into one slot"; 
       cout << endl << endl; 
       cout << "Enter your selection now: "; 
       cin >> menu_selection; 
       cout << endl; 
      } 

      cout << "*** DROPPING CHIP INTO SLOT " << slot_selection << " ***"; 
      cout << endl << endl; 
      cout << "PATH: ["; 

      if (position_chip = slot_selection) 
       for (int current_row= 1; current_row<= 12; current_row++) 
       { 
        double plink = (rand() % 2) - 0.5; 
        position_chip = position_chip + plink; 
        if (current_row< 12) 
        { 
         cout << position_chip << fixed << setprecision(2) << " "; 
        } 
        if (current_row== 12) 
        { 
         cout << position_chip << fixed << setprecision(2) << "]"; 
         cout << endl << endl; 
        } 

        if (position_chip > 8) 
        { 
         position_chip = position_chip - 1.0; 
        } 

        if (position_chip < 0) 
        { 
         position_chip = position_chip + 1.0; 
        } 

        if (current_row == 12 && position_chip == 0) 
        { 
         winning = REWARD_0; 
        } 

        else if (current_row == 12 && position_chip == 1) 
        { 
         winning = REWARD_1; 
        } 

        else if (current_row == 12 && position_chip == 2) 
        { 
         winning = REWARD_2; 
        } 

        else if (current_row== 12 && position_chip == 3) 
        { 
         winning = REWARD_3; 
        } 

        else if (current_row== 12 && position_chip == 4) 
        { 
         winning = REWARD_4; 
        } 

        else if (current_row== 12 && position_chip == 5) 
        { 
         winning = REWARD_5; 
        } 

        else if (current_row== 12 && position_chip == 6) 
        { 
         winning = REWARD_6; 
        } 

        else if (current_row== 12 && position_chip == 7) 
        { 
         winning = REWARD_7; 
        } 

        else if (current_row== 12 && position_chip == 8) 
        { 
         winning = REWARD_8; 
        } 
       } 
      cout << "WINNINGS: $" << winning << endl << endl; 

     } 

     if (menu_selection == 2) 
     { 
      cout << "*** DROP MULTIPLE CHIPS ***"; 
      cout << endl << endl; 
      cout << "How many chips fo you want to drop (>0)? "; 
      cin >> number_of_chips_drop; 
      cout << endl; 
      cout << "Which slot do you want to drop the chip in (0-8)? "; 
      cin >> slot_selection; 
      cout << endl; 

      if (slot_selection > 8 || slot_selection < 0) 
       { 
       cout << "INVALID SLOT."; 
       cout << endl << endl; 
       cout << "MENU: Please select one of the following options: "; 
       cout << endl << endl; 
       cout << "0 - Quit the program"; 
       cout << endl << endl; 
       cout << "1 - Drop a single chip into one slot"; 
       cout << endl << endl; 
       cout << "2 - Drop multiple chips into one slot"; 
       cout << endl << endl; 
       cout << "Enter your selection now: "; 
       cin >> menu_selection; 
       cout << endl; 
       } 

      if (slot_selection >= 0 && slot_selection <= 8) 
       { 
       for (int current_chip = 0; current_chip < number_of_chips_drop; current_chip++) 
        { 
        for (int current_row = 1; current_row <= 12; current_row++) 
         { 
          position_chip = slot_selection; 
          double plink = (rand() % 2) - 0.5; 
          position_chip = position_chip + plink; 

           if (current_row == 12 && position_chip == 0) 
           { 
            winning = REWARD_0; 
           } 

           else if (current_row == 12 && position_chip == 1) 
           { 
            winning = REWARD_1; 
           } 

           else if (current_row == 12 && position_chip == 2) 
           { 
            winning = REWARD_2; 
           } 

           else if (current_row == 12 && position_chip == 3) 
           { 
            winning = REWARD_3; 
           } 

           else if (current_row== 12 && position_chip == 4) 
           { 
            winning = REWARD_4; 
           } 

           else if (current_row== 12 && position_chip == 5) 
           { 
            winning = REWARD_5; 
           } 

           else if (current_row== 12 && position_chip == 6) 
           { 
            winning = REWARD_6; 
           } 

           else if (current_row== 12 && position_chip == 7) 
           { 
            winning = REWARD_7; 
           } 

           if (current_row== 12 && position_chip == 8) 
           { 
            winning = REWARD_8; 
           } 

         } 
        position_chip = slot_selection; 
        } 


       reward_average = reward_total/number_of_chips_drop; 

       cout << "Total Winnings on " << number_of_chips_drop << fixed << setprecision(2) << " chips: "; 
       cout << reward_total << endl << endl; 
       cout << "Average winnings per chip: " << reward_average << fixed << setprecision(2); 
       cout << endl << endl; 



       } 
     } 
    } 
    system("pause"); 
    return 0; 
} 
+0

for(;;)は何もしません。コードをステップ実行するためにデバッガを使用してください。 – brettmichaelgreen

+0

このような問題を解決する適切なツールはデバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低限、問題を再現する[最小、完全、および検証可能](http://stackoverflow.com/help/mcve)の例と、その問題を再現するためのデバッガ。 –

+0

@ Brett-MichaelGreen: 'for(;;)'は一般的なループです。そこからジャンプがなければ無限になります。ここにループ内に 'return'があります。 –

答えて

0

1つの明白な問題は、変数reward_totalのみ初期化時に設定されていることですが、論理ではない。

私はこのコードをデバッグしていませんが、これは少なくとも合計と平均報酬が0である理由の1つであると確信しています。

関連する問題