Plinko Problem - Winnings printing as 0

So I'm working on a Plink game program for a class and it's to practice our use of functions. We need a separate function for rewards, single chip drop, and multiple chip drop.

Anyway, the following is my program. And it runs properly except for one things. When multiple chip drop is selected, the output shows as $0.00 for total winnings and average winning per chip no matter how many chips are used. And unless I just have really bad luck and I win 0 every time, that's not what should be happening.

I can't find the error in the program that's making it do that.

Thanks for the help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;


const int PRIZE_SLOT_0 = 100;
const int PRIZE_SLOT_1 = 500;
const int PRIZE_SLOT_2 = 1000;
const int PRIZE_SLOT_3 = 0;
const int PRIZE_SLOT_4 = 10000;
const int PRIZE_SLOT_5 = 0;
const int PRIZE_SLOT_6 = 1000;
const int PRIZE_SLOT_7 = 500;
const int PRIZE_SLOT_8 = 100;


int winnings;

int reward (int chip_spot) {
    if (chip_spot == 0) {
        winnings = PRIZE_SLOT_0;
    } else if (chip_spot == 1) {
        winnings = PRIZE_SLOT_1;
    } else if (chip_spot == 2) {
        winnings = PRIZE_SLOT_2;
    } else if (chip_spot == 3) {
        winnings = PRIZE_SLOT_3;
    } else if (chip_spot == 4) {
        winnings = PRIZE_SLOT_4;
    } else if (chip_spot == 5) {
        winnings = PRIZE_SLOT_5;
    } else if (chip_spot == 6) {
        winnings = PRIZE_SLOT_6;
    } else if (chip_spot == 7) {
        winnings = PRIZE_SLOT_7;
    } else {
        winnings = PRIZE_SLOT_8;
    }
    return winnings;
}

int single_drop_simulator (int slot, int print_chip_spot) {
    double chip_spot = slot;
    for (int i = 0; i < 12; i++) {
        chip_spot += rand() % 2 ? .5: -.5;
        if (chip_spot > 8) {
            chip_spot -= 1.0;
        }
        if (chip_spot < 0) {
            chip_spot += 1.0;
        }
        if (print_chip_spot == 1) {
            cout << fixed << setprecision(1) << chip_spot << setw(4);
        }
    }
    reward(chip_spot);
    return winnings;
}


int multiple_drop_simulator(int number_of_chips, int slot) {
    double total_winnings;
    total_winnings = 0;
    for (int i = 0; i < number_of_chips; i++) {
        total_winnings += single_drop_simulator(slot, 0) ;
    }
    return total_winnings;
}

int main() {
    srand(time(NULL));
    double slot;
    bool end;
    do {
        //Menu
        cout << "\t\t\t\t** MENU **\nPlease choose one of the following operations:\n1 - Drop a single chip into one slot\n2 - Drop multiple chips into one slot\n3 - Quit the program\n";
        int operation;
        cin >> operation;
        
        if (operation > 3 || operation < 1) {
            cout << "Invalid selection. Please enter 1, 2, or 3 to select one of the options.\n\n";
            end = false;
            continue;
        }
        
        //Single Chip
        if (operation == 1) {
            cout << "\n\t\t\t\t\t** DROP SINGLE CHIP **\nPlease enter the number of the slot to place Plinko chip (0-8):\n";
            cin >> slot;
            if (slot < 0 || slot > 8) {
                cout << "Invalid selection. Please enter an integer from 0 to 8\n\n";
                end = false;
                continue;
            } else {
                cout << "Path: [";
                single_drop_simulator(slot, 1);
                cout << "]\nYou won $" << winnings << "\n\n";
            }
            end = false;
            continue;
        }
        
        //Multiple Chips
        if (operation == 2) {
            cout << "\n\t\t  **DROP MULTIPLE CHIPS**\nPlease enter number of chips to be placed: ";
            int number_of_chips;
            cin >> number_of_chips;
            if (number_of_chips <= 0) {
                cout << "Invalid number. Please enter a positive integer for number of chips.\n\n";
                end = false;
                continue;
            } else {
                cout << "Please enter the number of the slot to place Plinko chip (0-8): ";
                cin >> slot;
                if (slot < 0 || slot > 8) {
                    cout << "Invalid selection. Please enter an integer from 0 to 8\n\n";
                    end = false;
                    continue;
                }
                double total_winnings;
                multiple_drop_simulator(number_of_chips, slot);
                cout << "You won $" << fixed << setprecision(2) << total_winnings;
                cout << "\nAverage amount earned per chip $" << fixed << setprecision(2) << total_winnings /  number_of_chips << "\n\n";
            }
            end = false;
            continue;
        }
        if (operation == 3){
            cout << "Goodbye!";
        }
        end = true;
    } while (end == false);
    return 0;
}
.
Last edited on
122
123
                double total_winnings =
                multiple_drop_simulator(number_of_chips, slot);

You have a misunderstanding that is affecting you.

The purpose of a function is to not have to worry about global variables... like your int winnings on line 19.

To help fix your program (and receive full credit), start by deleting line 19. Then fix the reward() function to work without it: given a chip spot, return a winning dollar amount.

Now, at the end of single_drop_simulator(), you need to return the reward for landing in the given chip spot.

multiple_drop_simulator() is correct in every way:
- you have a local sum variable (line 64)
- updated by the result of a function call (line 67)
- returning its value to the caller (line 69)
Good job!

The remaining obstacles are lines 98 and 123, where you call the functions but completely ignore their results. We already fixed 122..123 above. What can you do for 98 (so that winnings exists when the compiler gets to line 99)? Hint: lines 123..124.

There are a number of other things that could be corrected, but nothing you really need to fret about at this point. Good job!

Hope this helps.
Awesome, I got it working, thanks!
It took me a little bit to understand what you were hinting at, but I think this is how you were suggesting it should look? Haha

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;


const int PRIZE_SLOT_0 = 100;
const int PRIZE_SLOT_1 = 500;
const int PRIZE_SLOT_2 = 1000;
const int PRIZE_SLOT_3 = 0;
const int PRIZE_SLOT_4 = 10000;
const int PRIZE_SLOT_5 = 0;
const int PRIZE_SLOT_6 = 1000;
const int PRIZE_SLOT_7 = 500;
const int PRIZE_SLOT_8 = 100;



int reward (int chip_spot) {
    if (chip_spot == 0) {
        return PRIZE_SLOT_0;
    } else if (chip_spot == 1) {
        return PRIZE_SLOT_1;
    } else if (chip_spot == 2) {
        return PRIZE_SLOT_2;
    } else if (chip_spot == 3) {
        return PRIZE_SLOT_3;
    } else if (chip_spot == 4) {
        return PRIZE_SLOT_4;
    } else if (chip_spot == 5) {
        return PRIZE_SLOT_5;
    } else if (chip_spot == 6) {
        return PRIZE_SLOT_6;
    } else if (chip_spot == 7) {
        return PRIZE_SLOT_7;
    } else {
        return PRIZE_SLOT_8;
    }
}

int single_drop_simulator (int slot, int print_chip_spot) {
    double chip_spot = slot;
    for (int i = 0; i < 12; i++) {
        chip_spot += rand() % 2 ? .5: -.5;
        if (chip_spot > 8) {
            chip_spot -= 1.0;
        }
        if (chip_spot < 0) {
            chip_spot += 1.0;
        }
        if (print_chip_spot == 1) {
            cout << fixed << setprecision(1) << chip_spot << setw(4);
        }
    }
    return reward(chip_spot);
}


int multiple_drop_simulator(int number_of_chips, int slot) {
    double total_winnings = 0;
    for (int i = 0; i < number_of_chips; i++) {
       total_winnings += single_drop_simulator(slot, 0);
    }
    return total_winnings;
}

int main() {
    srand(time(NULL));
    double slot;
    bool end;
    do {
        //Menu
        cout << "\t\t\t\t** MENU **\nPlease choose one of the following operations:\n1 - Drop a single chip into one slot\n2 - Drop multiple chips into one slot\n3 - Quit the program\n";
        int operation;
        cin >> operation;
        
        if (operation > 3 || operation < 1) {
            cout << "Invalid selection. Please enter 1, 2, or 3 to select one of the options.\n\n";
            end = false;
            continue;
        }
        
        //Single Chip
        if (operation == 1) {
            cout << "\n\t\t\t\t\t** DROP SINGLE CHIP **\nPlease enter the number of the slot to place Plinko chip (0-8):\n";
            cin >> slot;
            if (slot < 0 || slot > 8) {
                cout << "Invalid selection. Please enter an integer from 0 to 8\n\n";
                end = false;
                continue;
            } else {
                cout << "Path: [";
                int winnings = single_drop_simulator(slot, 1);
                cout << "]\nYou won $" << winnings << "\n\n";
            }
            end = false;
            continue;
        }
        
        //Multiple Chips
        if (operation == 2) {
            cout << "\n\t\t  **DROP MULTIPLE CHIPS**\nPlease enter number of chips to be placed: ";
            int number_of_chips;
            cin >> number_of_chips;
            if (number_of_chips <= 0) {
                cout << "Invalid number. Please enter a positive integer for number of chips.\n\n";
                end = false;
                continue;
            } else {
                cout << "Please enter the number of the slot to place Plinko chip (0-8): ";
                cin >> slot;
                if (slot < 0 || slot > 8) {
                    cout << "Invalid selection. Please enter an integer from 0 to 8\n\n";
                    end = false;
                    continue;
                }
                double total_winnings = multiple_drop_simulator(number_of_chips, slot);
                cout << "You won $" << fixed << setprecision(2) << total_winnings;
                cout << "\nAverage amount earned per chip $" << fixed << setprecision(2) << total_winnings /  number_of_chips << "\n\n";
            }
            end = false;
            continue;
        }
        if (operation == 3){
            cout << "Goodbye!";
        }
        end = true;
    } while (end == false);
    return 0;
}
exactly right, well done. :O)
Topic archived. No new replies allowed.