### 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....

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````#include #include #include 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: "<> opt; if (opt =='0') { cout <<"You bet 1\$ each time for 0.Heads.."<

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:

 ``12`` ``````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?

 ``12`` ``````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.

 ``123456789101112`` ``````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.