Almost finished with homework, but need a little help

I've figure out most of the mistakes I made in the code, but I've been looking at this code too long and honestly can't figure out what it missing. Can anyone help me? Thank you!! I'm getting this error:

main.cpp: In function 'int drop_single_chip(int, int)':
main.cpp:143:1: warning: control reaches end of non-void function [-Wreturn-type]
}

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>

using namespace std;

const double PRIZE_0 = 100.0;
const double PRIZE_1 = 500.0;
const double PRIZE_2 = 1000.0;
const double PRIZE_3 = 0.00;
const double PRIZE_4 = 10000.0;
const double PRIZE_5 = 0.00;
const double PRIZE_6 = 1000.0;
const double PRIZE_7 = 500.0;
const double PRIZE_8 = 100.0;
const int QUIT = 0;
//int winnings = 0;


void root_menu()
{

   cout << endl;
   cout << "0 - Quit the program" << endl;
   cout << "1 - Drop a single chip into one slot" << endl;
   cout << "2 - Drop multiple chips into one slot" << endl;
   cout << endl;
   cout << "Enter your selection now: " << endl << endl;
}

int prize_money_func(int location_of_chip)
{
   int reward = 0;
   switch (location_of_chip)
   {
   case 0:
       location_of_chip = 0;
       reward = PRIZE_0;
       break;
   case 1:
       location_of_chip = 1;
       reward = PRIZE_1;
       break;
   case 2:
       location_of_chip = 2;
       reward = PRIZE_2;
       break;
   case 3:
       location_of_chip = 3;
       reward = PRIZE_3;
       break;
   case 4:
       location_of_chip = 4;
       reward = PRIZE_4;
       break;
   case 5:
       location_of_chip = 5;
       reward = PRIZE_5;
       break;
   case 6:
       location_of_chip = 6;
       reward = PRIZE_6;
       break;
   case 7:
       location_of_chip = 7;
       reward = PRIZE_7;
       break;
   case 8:
       location_of_chip = 8;
       reward = PRIZE_8;
       break;
   }
   return(reward);
}

int drop_single_chip(int slot_number, int option) {
   double location_of_chip = slot_number;
   //int winnings = 0;
   for (int i = 0; i < 12; i++)
   {
      int winnings = prize_money_func(location_of_chip);
      return winnings;
      int random_number = rand();
      if (random_number % 2 == 1)
       {
           location_of_chip += 0.5;
           if (location_of_chip > 8)
           {
               location_of_chip -= 1.0;
           }
       }
       else
       {
           location_of_chip -= 0.5;
           if (location_of_chip < 0)
           {
               location_of_chip += 1.0;
           }
       }
       if (option == 1)
       {
           cout << " " << location_of_chip;
          
       }
          
   }
}

int drop_multiple_chips(int slot_number, int number_of_chips, int option)
{
   double total_winnings = 0;
   
   for (int i = 0; i < number_of_chips; i++)
   {
       int winnings = drop_single_chip(slot_number, option);
       total_winnings += winnings;
   }
   return total_winnings;
}
void drop_multiple_chips_in_each_slot(int number_of_chips_per_slot, int option)
{
   for (int slot_number = 0; slot_number < 9; slot_number++)
   {
       double total_winnings = drop_multiple_chips(slot_number, number_of_chips_per_slot, option);
       cout << fixed << setprecision(2);
       cout << "Total winnings on slot " << slot_number << " " << "chips: " << total_winnings << endl;
       double average_winnings_per_chip = total_winnings / number_of_chips_per_slot;
       cout << "Average winnings per chip: " << average_winnings_per_chip << endl << endl;
   }
}

int main() {
   
   int option;
   srand(time(0));
   
   cout << "Welcome to the Plinko simulator!" << endl << endl;

   cout << "Menu: Please select one of the following options:" << endl;
   
   do {
       root_menu();
       cin >> option;
       if (cin.fail()) {
           cin.clear();
           cin.ignore(1000);
           cout << "Invalid selection. Please enter 0, 1, or 2" << endl;
       }
       else {
           switch (option)
           {
           case 1:
               
               cout << "*** Drop single chip ***" << endl << endl;

               cout << endl << "which slot do you want to drop the chip(s) in (0-8)? " << endl;
               int slot_number;
               cin >> slot_number;
               if (cin.fail())
               {
                   cin.clear();
                   cin.ignore(1000);
                   cout << "Invalid slot." << endl;
               }
               else if (slot_number > 8 || slot_number < 0)
               {
                   cout << "Invalid slot." << endl;
               }
               else
               {
                  // double slot_number_double = slot_number:
                   cout << fixed << setprecision(1);
                   int winnings = (slot_number);
                   cout << "winnings: $" << winnings << endl << endl;
               }
               break;
            
           case 2:
               cout << endl << "*** Drop multiple chips ***" << endl << endl;
               cout << "How many chips do you want to drop (>0)? " << endl << endl;
               int number_of_chips;
               cin >> number_of_chips;
               if (cin.fail())
               {
                   cin.clear();
                   cin.ignore(1000);
                   cout << "Invalid number of chips." << endl;
               }
               else if (number_of_chips < 1)
               {
                   cout << "Invalid number of chips." << endl;
               }
               else
               {
                   cout << endl << "Which slot do you want to drop the chip(s) in (0-8)? " << endl;
                   int slot_number;
                   cin >> slot_number;
                   if (cin.fail())
                   {
                       cin.clear();
                       cin.ignore(1000,' ');
                       cout << "Invalid slot. Please enter an integer between 0 and 8" << endl;
                   }
                   else if (slot_number > 8 || slot_number < 0)
                   {
                       cout << "Invalid slot. Please enter an integer between 0 and 8" << endl;
                   }
                   else
                   {
                       double total_winnings = drop_multiple_chips(slot_number, number_of_chips, option);
                       cout << fixed << setprecision(2);
                       cout << "Total winnings on " << number_of_chips << " chips: $" << total_winnings << endl;
                       double average_winnings_per_chip = total_winnings / number_of_chips;
                       cout << "Average winnings per chip: $" << average_winnings_per_chip << endl << endl;
                   }
               }
               break;
               
           case 3:
               cout << endl << "How many chips do you want to drop (>0)? ";
               int number_of_chips_per_slot;
               cin >> number_of_chips_per_slot;
               cout << endl;
               if (cin.fail())
               {
                   cin.clear();
                   cin.ignore(1000, '\n');
                   cout << "Invalid number of chips.";
               }
               else if (number_of_chips_per_slot <= 0)
               {
                   cout << "Invalid number of chips.";
               }
               else
               {
                   cout << "*** SEQUWNRIALLY DROP MULTIPLE CHIPS ***" << endl << endl;
                   drop_multiple_chips_in_each_slot(number_of_chips_per_slot, option);
               }
               break;
               
           case 4:
           
               cout << "Goodbye!" << endl;
               return 0;
               break;
               
           default:
          
               cout << endl << "Invalid selection. Please enter 0, 1, or 2" << endl;
               break;
           }
       }
   }
   while (option != 0);
   //system("pause");
   return 0;
}
What compiler are you using? http://cpp.sh/ compiles and runs the code without problems, albeit your code is still has issues.

On the topic of your program flow, case 4 even exist? It's not documented at all (neither within your comments or within your program's UI), and it doesn't do anything useful. Did you forget to remove it?

Maybe you erroneously implemented case 0 under it. Try replacing the '4' with a '0', and your program should behave as expected (currently inputting a '0' activates the default case, which I'm assuming wasn't your intent).
Last edited on
int drop_single_chip(int slot_number, int option) {
double location_of_chip = slot_number;
//int winnings = 0;
for (int i = 0; i < 12; i++)
{
int winnings = prize_money_func(location_of_chip);
return winnings;

looks like this function is confusing the compiler. you can return in the middle, but all paths in the function need to return a value and all paths need to be reachable or you get a warning.

Thank you both!! That helped a lot!
Thanks to your help, I got the program running, but my path and winnings outputs are wrong. winnings will not output anything and the path isn't correct.

My program outputs when two is entered:

Path: [ 2.5 3 3.5 3 3.5 3 2.5 3 2.5 3 2.5 2]
Winnings: $0

it is supposed to output:

Path: [2.0 1.5 1.0 1.5 2.0 1.5 1.0 1.5 1.0 1.5 2.0 1.5 2.0]
Winnings: $1000.00


Post your new code so we can have a look. Don't edit your original post, put the new code in a reply.
Sorry, I should've thought of that. Here is the new code:

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>

using namespace std;

const double PRIZE_0 = 100.0;
const double PRIZE_1 = 500.0;
const double PRIZE_2 = 1000.0;
const double PRIZE_3 = 0.00;
const double PRIZE_4 = 10000.0;
const double PRIZE_5 = 0.00;
const double PRIZE_6 = 1000.0;
const double PRIZE_7 = 500.0;
const double PRIZE_8 = 100.0;
const int QUIT = 0;
int option = 0;
const int ROW_OF_PEGS = 12;



void root_menu()
{
   cout << endl;
   cout << "0 - Quit the program" << endl;
   cout << "1 - Drop a single chip into one slot" << endl;
   cout << "2 - Drop multiple chips into one slot" << endl;
   cout << endl;
   cout << "Enter your selection now: " << endl; 
   //if(option == 0) {
     // cout << "Goodbye!";
   //}
}
int prize_money_func(int location_of_chip)
{
   int reward = 0;
   switch (location_of_chip)
   {
   case 0:
       location_of_chip = 0;
       reward = PRIZE_0;
       break;
   case 1:
       location_of_chip = 1;
       reward = PRIZE_1;
       break;
   case 2:
       location_of_chip = 2;
       reward = PRIZE_2;
       break;
   case 3:
       location_of_chip = 3;
       reward = PRIZE_3;
       break;
   case 4:
       location_of_chip = 4;
       reward = PRIZE_4;
       break;
   case 5:
       location_of_chip = 5;
       reward = PRIZE_5;
       break;
   case 6:
       location_of_chip = 6;
       reward = PRIZE_6;
       break;
   case 7:
       location_of_chip = 7;
       reward = PRIZE_7;
       break;
   case 8:
       location_of_chip = 8;
       reward = PRIZE_8;
       break;
   }
  return reward;
}

int drop_single_chip(int slot_number, int option) {
   double location_of_chip = slot_number;
   double winnings = 0.0;
   for (int i = 0; i < ROW_OF_PEGS; i++)
   {
      
      
      int random_number = rand();
      if (random_number % 2 == 2)
       {
           location_of_chip += 0.5;
           if (location_of_chip > 8)
           {
               location_of_chip -= 1.0;
           }
       }
       else
       {
           location_of_chip -= 0.5;
           if (location_of_chip < 0)
           {
               location_of_chip += 1.0;
           }
       }
       if (option == 1)
       {
           cout << " " << location_of_chip;
          
       }
         
   }
    winnings = prize_money_func(location_of_chip);
    return winnings; 
   //return(reward);
} 
 
int drop_multiple_chips(int slot_number, int number_of_chips, int option)
{
   double total_winnings = 0.0;
   
   for (int i = 0; i < number_of_chips; i++)
   {
       double winnings = drop_single_chip(slot_number, option);
       total_winnings += winnings;
   }
   return total_winnings;
}
void drop_multiple_chips_in_each_slot(int number_of_chips_per_slot, int option)
{
   for (int slot_number = 0; slot_number < 9; slot_number++)
   {
       double total_winnings = drop_multiple_chips(slot_number, number_of_chips_per_slot, option);
       cout << fixed << setprecision(2);
       cout << "Total winnings on slot " << slot_number << " " << "chips: " << total_winnings << endl;
       double average_winnings_per_chip = total_winnings / number_of_chips_per_slot;
       cout << "Average winnings per chip: " << average_winnings_per_chip << endl << endl;
   }
}

int main() {
     
   double winnings = 0.0;
   int option = 0;
   srand(time(NULL));
   
   cout << "Welcome to the Plinko simulator!" << endl << endl;
   cout << "Menu: Please select one of the following options:" << endl;
  
  
   
   do {
       root_menu();
       cin >> option;
       
       if (cin.fail()) {
           cin.clear();
           cin.ignore(1000);
           cout << "Invalid selection. Please enter 0, 1, or 2" << endl;
       }
       else {
           switch (option)
           {
           case 1:
               int slot_number;
               cout << "*** Drop a single chip ***" << endl;
               cout << endl << "Which slot do you want to drop the chip in (0-8)? " << endl;
               cin >> slot_number;
               cout << "*** Dropping chip into slot " << slot_number << " ***" << endl;
               
               
               
               
               
               if (cin.fail())
               {
                   cin.clear();
                   cin.ignore(1000);
                   cout << "Invalid slot." << endl;
               }
               else if (slot_number < 0 || slot_number > 8)
               {
                   cout << "Invalid slot." << endl;
               }
               else
               {
               
                cout << "Path: [";
                drop_single_chip(slot_number, 1);
                cout << "]" << endl << "Winnings: $" << winnings << endl << endl;
                   cout << fixed << setprecision(2);
                cout << "Menu: Please select one of the following options:" << endl;
         
               }
               break;
            
           case 2:
               cout << endl << "*** Drop multiple chips ***" << endl << endl;
               cout << "How many chips do you want to drop (>0)? " << endl << endl;
               int number_of_chips;
               cin >> number_of_chips;
               if (cin.fail())
               {
                   cin.clear();
                   cin.ignore(1000);
                   cout << "Invalid number of chips." << endl;
               }
               else if (number_of_chips < 1)
               {
                   cout << "Invalid number of chips." << endl;
               }
               else
               {
                   cout << endl << "Which slot do you want to drop the chip in (0-8)? " << endl;
                   int slot_number;
                   cin >> slot_number;
                   if (cin.fail())
                   {
                       cin.clear();
                       cin.ignore(1000);
                       cout << "Invalid slot. Please enter an integer between 0 and 8" << endl;
                   }
                   else if (slot_number > 8 || slot_number < 0)
                   {
                       cout << "Invalid slot. Please enter an integer between 0 and 8" << endl;
                   }
                   else
                   {
                       double total_winnings = drop_multiple_chips(slot_number, number_of_chips, option);
                       cout << fixed << setprecision(2);
                       cout << "Total winnings on " << number_of_chips << " chips: $" << total_winnings << endl;
                       double average_winnings_per_chip = total_winnings / number_of_chips;
                       cout << "Average winnings per chip: $" << average_winnings_per_chip << endl << endl;
                   }
               }
               break;
               
           case 3:
               cout << endl << "How many chips do you want to drop (>0)? ";
               int number_of_chips_per_slot;
               cin >> number_of_chips_per_slot;
               cout << endl;
               if (cin.fail())
               {
                   cin.clear();
                   cin.ignore(1000);
                   cout << "Invalid number of chips.";
               }
               else if (number_of_chips_per_slot <= 0)
               {
                   cout << "Invalid number of chips.";
               }
               else
               {
                   cout << "*** SEQUWNRIALLY DROP MULTIPLE CHIPS ***" << endl << endl;
                   drop_multiple_chips_in_each_slot(number_of_chips_per_slot, option);
               }
               break;

           case 0:

               cout << "Goodbye!";
               return 0;
               break;

           default:

               cout << endl<< "Invalid selection. Please enter 0, 1, or 2" << endl;
               break;
           }
       }
   }
   while (option != 0);
   //system("pause");
   return 0;

}
Line 42. Since you're inside case 0, location_of_chip is 0. You you're just setting it to the value it already has. Same comment for the other cases. In fact, you could greatly reduce the size of prize_money_func() with this:
1
2
3
4
5
6
int prize_money_func(int location_of_chip)
{
    static const int PRIZES[] = {100, 500, 1000, 0, 10000, 0, 1000, 500 100};
    int reward = PRIZES[location_of_chip];
    return reward;
}

Line 89: Can you give an example of a number that, when divided by 2, has a remainder of 2?
Lines 188 should be winnings = drop_single_chip(slot_number, 1);

You can shorten this program by almost 100 lines and make it more robust by creating a function to prompt the user for a number:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Prompt the user for a number between low and high (inclusive).
// This prints "prompt" and then reads a number from the user.  If the user
// doesn't enter a number or enters an invalid number, then print errorMessage,
// clear the stream, read through the next line and try again.
int
getNum(const char *prompt, const char *errorMessage, int low, int high)
{
    int result;
    while (true) {
        cout << prompt << flush;
        if ((cin >> result) && result >= low && result <= high) {
            return result;
        }
        cout << errorMessage << endl;
        cin.clear();
        cin.ignore(1000000000, '\n'); // skip the rest of the line
    }
}

Now, for example, you can replace lines 166-185 with:
1
2
3
slot_number = getNum("Which slot do you want to drop the chip in (0-8)? ",
                                 "Invalid slot.\n",
                                 0, 8);


You can do the same everywhere else that you prompt for a number.
I tried those and I am honestly still stuck. My winnings output is all over the place and my path output still is not correct. I think going over and over this for a week has made it so I can't think ....
Did you change lines 89 and 188 as I suggested? When I do that it seems to work:
Welcome to the Plinko simulator!

Menu: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now:
1
*** Drop a single chip ***

Which slot do you want to drop the chip in (0-8)?
2
*** Dropping chip into slot 2 ***
Path: [ 2.5 3 2.5 3 2.5 2 2.5 2 1.5 1 0.5 1]
Winnings: $500

Menu: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now:
2

*** Drop multiple chips ***

How many chips do you want to drop (>0)?

4

Which slot do you want to drop the chip in (0-8)?
5
Total winnings on 4 chips: $31000.00
Average winnings per chip: $7750.00


0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now:
3

How many chips do you want to drop (>0)? 4

*** SEQUWNRIALLY DROP MULTIPLE CHIPS ***

Total winnings on slot 0 chips: 2100.00
Average winnings per chip: 525.00

Total winnings on slot 1 chips: 12000.00
Average winnings per chip: 3000.00

Total winnings on slot 2 chips: 1600.00
Average winnings per chip: 400.00

Total winnings on slot 3 chips: 21100.00
Average winnings per chip: 5275.00

Total winnings on slot 4 chips: 20000.00
Average winnings per chip: 5000.00

Total winnings on slot 5 chips: 1600.00
Average winnings per chip: 400.00

Total winnings on slot 6 chips: 1500.00
Average winnings per chip: 375.00

Total winnings on slot 7 chips: 3000.00
Average winnings per chip: 750.00

Total winnings on slot 8 chips: 2100.00
Average winnings per chip: 525.00


0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now:
0
Goodbye!

I did, and this is the outputs I am getting:

4. Compare output
0/7
Input
1
2
0
Your output starts with Welcome to the Plinko simulator!

Menu: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now:
*** Drop a single chip ***

Which slot do you want to drop the chip in (0-8)?
*** Dropping chip into slot 2 ***
Path: [ 2.5 3 2.5 2 1.5 2 2.5 3 2.5 3 2.5 2]
Winnings: $2

Menu: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now:
Goodbye!
Expected output starts with Welcome to the Plinko simulator!

Menu: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now:
*** Drop a single chip ***

Which slot do you want to drop the chip in (0-8)?
*** Dropping chip into slot 2 ***
Path: [2.0 1.5 1.0 1.5 2.0 1.5 1.0 1.5 1.0 1.5 2.0 1.5 2.0]
Winnings: $1000.00

Menu: Please select one of the following options:

0 - Quit the program
1 - Drop a single chip into one slot
2 - Drop multiple chips into one slot

Enter your selection now:
Goodbye!
Please post your new code.
Topic archived. No new replies allowed.