Random Generator problem C++

Well I get stuck and I need your help for a gambler program...
the user select 0.Heads or 1.Tails...
the program automatic randomize 10 times a variable and calculating the total amount of the gambler.. The gambler starts with 50$ each time that he/she wins +1$ if he/she lose -1$... By serious problem is that I get stuck how to return the values form case 1 into case 2.... :-/ form example::
first loop::
user wins...!!! = total amount = 51$ (50+1)
user wins...!!! = total amount = 52$ (51+1)
user lose...!!! = total amount = 51$ (52-1)
user wins...!!! = total amount = 52$ (51+1)

here is my HALF code only for the selection 0.Heads....


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
#include <iostream>
#include <time.h>
#include <ctime>

using namespace std;



int main() {

	srand((time(0)));

    int  TOTAL=50, num, save_balance, i=0;
    char opt;
    int count = false;

    int counter=1;

	cout <<"Please select one of the options: "<<endl;
	cout <<"0.Heads"<<endl;
	cout <<"1.Tails"<<endl;
    cout <<"Option: ";
    cin >> opt;

    if (opt =='0')
    {
    	cout <<"You bet 1$ each time for 0.Heads.."<<endl;
         while (i<=10)
    	{
    	 i++;
    	 num = rand() % + 2;
         save_balance = TOTAL;

    	 switch (num)
    	 {
    	 case 0:
    		 if (!count)
         {
                         counter++;
    		         counter--;
    		         count =true;
         }
    	             else
    	                      {
    		                     counter++;
    		     		         save_balance = TOTAL + counter;
    		     	     		 cout <<"Win: "<<save_balance<<endl;
    	                                           }

    	 case 1:     if (!count)
    	 {

    		         counter--;
                         counter++;
                         count = true;
    	 }
    	             else
    	                        {
                                         counter--;
    		                         save_balance = TOTAL - counter;
    		      		     	 cout <<"Lose: "<<save_balance<<endl;

   break;
    	                                            }

    	}
    }
	return 0;

}



Last edited on
I'm a little confused by your code, you seem to be making things harder than they need to be.

Is the supposed to be able to choose heads or tails before each toss of the coin? Or do they make the choice and then the coin gets flipped 10 times based on that choice? (that's what the current code would suggest).

A few things to point out here:

1
2
counter--;
counter++;

Not quite sure what's going on here. This seems like a pointless couple of lines because the value is being incremented then immediately decremented, leaving you with the same value as before those two lines run.

 
while (i<=10)

You do realise that this is going to run eleven times, right?

1
2
counter--;
save_balance = TOTAL - counter;

You're making your program much harder than it needs to be here. Every time you reference save_balance, you're adding TOTAL to it, plus some sort of operation with the counter. Once save_balance has been assigned TOTAL, it contains 50. You don't need to keep assigning TOTAL to it. Also, there's a much easier way to keep track of the total.

I would go for something along the lines of the following (I'm going to assume that heads or tails is only chosen once and the coin is tossed 10 times based on that choice, as per your current setup):

I originally wrote out a full code solution for this but thought it'd be better if I wrote some pseudo-code that you can figure out yourself. I've saved the solution in case you need to see it later.

1
2
3
4
5
6
7
8
9
10
11
12
Declare variables representing input, current cash and coin face
Set cash to 50
Get input from user
for (number of tosses)
{
   generate a random number for coin face
   if (user input is equal to coin face)
      increment cash
   else
      decrement cash
}
Display end total (output cash)


As I've said, I've written a full solution but I'm not sure it'll be beneficial to you if I just go ahead and post the whole thing. See if you can get anything from the example above and if you're still stuck let me know and I'll help further.
Last edited on
Topic archived. No new replies allowed.