Need help based on a written code

Create a coin-flipping game. Ask the user how many times to flip the coin, and use the random
function to determine heads or tails each time a coin is flipped.
Assume the user starts with $50. Every time the coin is flipped calculate the total (heads +$10,
tails -$10). Create another function to test if the user has gone broke yet (THIS FUNCTION
MUST RETURN A BOOLEAN TRUE/FALSE VALUE). End the program when the user is broke or
when the coin was flipped the number of times the user had specified.
Display:
1) how many times the coin was flipped,
2) the number of times “heads” was the result,
3) the number of times “tails” was the result, and
4) how much money the user has at the end


#include <iostream>
using namespace std;
# include <ctime>

int coin()
{
int flip;
flip= rand() % 2 + 1;// assign random numbers
if (flip == 1)
cout << "The flip was heads." << endl;
else
cout << "The flip was tails." << endl;
//end if
return (flip);
}

int main ()
{
int NUM_FLIPS = 100;
int count, face, heads = 0, tails = 0;

// initialize the random number generator
srand(static_cast<int>(time(0)));

// generate and count the number of heads and tails
for (int count=1; count <= NUM_FLIPS; count++)
{
face = coin();
if (face == 1)
heads++;
else
tails++;
cout << face << endl;
}

cout << "The number flips: " << NUM_FLIPS << endl;
cout << "The number of heads: " << heads << endl;
cout << "The number of tails: " << tails << endl;
}

I need help on how to add a balance that subtracts 10 dollars of heads or adds 10 to your balance if tails.




Create a coin-flipping game.

Ask the user how many times to flip the coin, and use the random
function to determine heads or tails each time a coin is flipped.

Assume the user starts with $50.
Every time the coin is flipped calculate the total (heads +$10, tails -$10).

Create another function to test if the user has gone broke yet (THIS
FUNCTION MUST RETURN A BOOLEAN TRUE/FALSE VALUE).

End the program when the user is broke or when the coin was flipped the
number of times the user had specified.
So your pseudo code looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
initialize_random_generator()

n = get_number_of_flips()

balance = 50
for ( count = 0; count < n && ! broke(balance); ++count )
    heads = random( 1 .. 2 )
    if ( heads == 1 )
        balance += 10
    else
        balance -= 10
    endif
endfor
Last edited on
Topic archived. No new replies allowed.