-2

so this is my entire code. The problem is that when I try to drop multiple chips and then print out the total winnings and the average winning per chip all I get is 0. I am really stuck, brand new to computer science and coding and all out of options. I'm sure its easy to fix, maybe I'm missing something or did something wrong. But I can't for the life of me figure out how to fix it. Please help! Thank you!!!!!!

(and I know it would be easier to use functions but in this assignment we are told not to use functions)

#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; 
}
  • for (;;) does nothing, use a debugger to step through your code – brettmichaelgreen Oct 08 '16 at 01:01
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Oct 08 '16 at 01:02
  • @Brett-MichaelGreen: `for(;;)` is a general loop, infinite if there is no jump out of it. Here there is a `return` within the loop. – Cheers and hth. - Alf Oct 08 '16 at 01:03
  • @Upvoter May you explain yourself leaving a comment please! – πάντα ῥεῖ Oct 08 '16 at 01:03
  • hi sorry, so when I run my program the menu options 0 (to quit the program) and 1 (to drop a single chip then report the path of the chip randomly falling down the board, and the prize money won from that chip) work perfectly fine. – Christie Yochim Oct 08 '16 at 01:07
  • I can't think of any way this question can help others, but I can't think of a valid reason to vote to close either. If you have a debugger, use it. If not, add trace output statements etc. to find out what's (not) going on. – Cheers and hth. - Alf Oct 08 '16 at 01:08
  • Recommendation: read up on `switch` statements. The should help reduce the code a little. Moving the various menu cases into separate functions would make the code easier to manage as well. – user4581301 Oct 08 '16 at 01:11
  • The problem comes when I choose 2 from the menu, to drop multiple chips into the same slot. what I want my program to do is drop each chip into the same starting slot and fall in a different random path each time. and I want the winning from each chip to be added together for a total winnings from dropping all the chips. That is where my problem is. When i run my program to drop any number of chips, regardless of the starting slot, I get 0 as the total amount of money won from that round. And I don't know how to fix that. I also need the total amount of money to reset after it is printed – Christie Yochim Oct 08 '16 at 01:11
  • @Cheersandhth.-Alf _"but I can't think of a valid reason to vote to close either."_ The valid close reason is _Missing MCVE_ as pointed out from my canned comment. The MCVE isn't complete without the debugging observations. – πάντα ῥεῖ Oct 08 '16 at 01:14

1 Answers1

0

One obvious problem is that the variable reward_total is only being set on initialization, but not in the logic.

I haven't debugged this code, but I'm sure this is at least one of the reasons you are receiving 0 for total and average rewards.