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?
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
usingnamespace 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;
}
}