Coin flip simulator repeating until heads appears

So I'm writing a program that pays out an amount 2n where n is the number of the toss in which the first Heads appears. I'm pretty sure I have everything correct except for repeating the flips until heads appears and possibly something with the average payout. I'm thinking that a while loop is needed?

output is just showing:
T
H

instead of:
TH
TTH
H
etc...


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
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
	srand(time(NULL));
	int n;
	int flip;
	int count = 0;
	double winnings, avgPayout, total;
	char face;

	cout << fixed << setprecision(2);
	for (count = 1; count <= 10; count++)
	{
		n = 0;
		winnings = 0;
		total = 0;
		
			flip = rand() % 2 + 1;
			if (flip == 1)
			{
				face = 'H';
				n++;
				winnings = pow(2, n);
				total += winnings;

			}
			else
				face = 'T';
		
		
		cout << face << '\t' << "You win $" << winnings << endl;
	}
	
	avgPayout = (total) / 10.0;
	cout << endl;
	cout << "The average payout was $" << avgPayout << endl;

}
Last edited on
I get an error on line 28 that says pow is out of scope.
Last edited on
closed account (48T7M4Gy)
repeating the flips until heads appears

And therein lies a piece of pseudocode you can act on.

Hint: a while loop is a way of archiving the goal - try it :)
closed account (48T7M4Gy)
A small observation.

Each time you go through the loop n increments by 1 and winnings go to 2^n. Instead of using pow why not winnings *= 2; and forget about n too.
Last edited on
I get an error on line 28 that says pow is out of scope.


It seems to be compiling fine for me.

Hint: a while loop is a way of archiving the goal - try it :)


And I've tried placing a while loop in and out of the for loop, but am starting to get confused on what its condition should be.

do.......while(flip == 'T') ?????????

I'm lost...

Last edited on
Instead of using pow why not winnings *= 2; and forget about n too.


Doing this outputs all of my winnings as 0.
closed account (48T7M4Gy)
Doing this outputs all of my winnings as 0.

not if you initialize, int n = 1; :)
closed account (48T7M4Gy)
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
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
	srand(time(NULL));
	int flip;
	int  winnings = 1;
	
	double avgPayout = 0;
	double total = 0;
	char face;

	cout << fixed << setprecision(2);
	
for ( int i = 0; i < 50; i++) // <- play 50 games just for testing purposes
{
    winnings = 1;
    total = 0;
    
    do
	{
	    flip = rand() % 2;
	    
	    if (flip == 0)
		{
			face = 'H';
			winnings *= 2;
			total += winnings;
			cout << face << " You win $" << winnings << "\tYour total so far is $" << total << endl;
		}
		else
		{
			face = 'T';
			cout << face << " End of game" << endl;
		}		
	}while ( flip != 1);
	
	avgPayout = (total) / 10.0;
	cout << "The average payout was $" << avgPayout << endl << endl;
}

}
Topic archived. No new replies allowed.