Help with this C++ exercise please?

Hello,

I am working on some C++ exercises. I'm a beginner and having a lot of trouble moving forward with this one:

"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 their winnings (win +$10, lose -$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 either when they’ve gone 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 they’ve won/lost."

So far, this is the code I've written:

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

void displayFlip(int z)
{
	cout << "Times flipped:" << endl;
	cout << z << endl;
}

int main()
{
	cout << "How many times would you like to flip the coin?" << endl;
	int x;
	int z;
	cin >> z;
	displayFlip(z);
	cout << "Result:" << endl;

	srand(time(0));
	for (int x = 0; x < z; x++)
		cout << 1 + (rand() % 2) << endl;

	cout << "Heads returned:" << endl;

	cout << "Tails returned:" << endl;

}


Please feel free to edit or completely take out some parts. I'm trying to figure out how to work with random numbers--and making games like these. Please stick within the headers I listed though. Thank you for your replies!
First off, you need a couple of variables to count the number of heads and tails.
1
2
3
 
  int heads = 0;
  int tails = 0;


It's easiest if you assign your random number to a variable.
23
24
25
26
27
28
29
30
31
  for (int x = 0; x < z; x++)
  {  int flip;
      flip = rand() %2;
      cout << flip << endl;
      if (flip)
        heads++;
     else
       tails++;
  }


BTW, your variable names x and z suck. Your loop declaration at line 23 hides the variable x at line 16 for the range of the loop.
When does one "win" or "loose"?


You have only one statement in the loop. You do need more. Actually, a while-loop would be more intuitive.

Can you write a separate function to test being broke?

Perhaps the amount of cash should be stored somewhere?

Is it true that heads+tails==all flips? Does that have some implications?

Can you increment the value of a variable? (Your code has one example, but do you know that?)
OKAY, that makes a bit of sense, but how do I go about solving the rest of this problem?
I just need help with this part now:

"Assume the user starts with $50. Every time the coin is flipped calculate their winnings (win +$10, lose -$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 either when they’ve gone broke or when the coin was flipped the number of times the user had specified. "

I'm really unsure on how to go about testing it after each flip. The way I set it up does all the flips at once. I also don't know about the boolean part.

To answer your question, one wins when it's 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
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
	cout << "How many times would you like to flip the coin?" << endl;
	int z;
	cin >> z;
	cout << "Times flipped:" << endl;
	cout << z << endl;
	cout << "Result:" << endl;
	int heads = 0;
	int tails = 0;
	srand(time(0));
	for (int x = 0; x < z; x++)
	{
		int flip;
		flip = rand() % 2;
		cout << flip << endl;
		if (flip)
			heads++;
		else
			tails++;
	}
	cout << "Heads returned:" << endl;
	cout << heads << endl;
	cout << "Tails returned:" << endl;
	cout << tails << endl;
	int winnings;
	winnings = 50 + (heads * 10) - (tails * 10);
	cout << "Winnings:" << endl;
	cout << winnings << endl;
}
Last edited on
Assume the user starts with $50

That's a pretty strong hint that you should declare and initialize winnings BEFORE your loop.

Do you know how to write a bool function? It should have a return type of bool.

1
2
3
4
5
6
bool is_broke (int winnings)
{  if (winnings <= 0)
      return true;
    else
      return false;
}


As part of each flip (lines 24,26), you need to increment or decrement winnings by $10.
Then after the flip (before line 27), call your is_broke() function:
1
2
3
4
5
     if (is_broke(winnings))
     {  cout << "You're broke" << endl;
         return 0;  // Game over
     }
      cout << "You now have $" << winnings << endl;

Last edited on
I wrote it like this, it's not running however

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 <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
	cout << "How many times would you like to flip the coin?" << endl;
	int z;
	cin >> z;
	cout << "Times flipped:" << z << endl;
	cout << "Result:" << endl;
	int heads = 0;
	int tails = 0;
	srand(time(0));
	for (int x = 0; x < z; x++)
	{
		int flip;
		flip = rand() % 2;
		cout << flip << endl;
		if (flip)
			heads++;
		else
			tails++;
	}
	cout << "Heads returned:" << heads << endl;
	cout << "Tails returned:" << tails << endl;
	int winnings;
	winnings = 50 + (heads * 10) - (tails * 10);
	bool is_broke(int winnings);
	{
		if (winnings <= 0)
			return true;
		else
			return false;
	}
	if (is_broke(winnings))
	{
		cout << "You're broke" << endl;
	}
	cout << "You now have $" << winnings << endl;

}
how much money they’ve won/lost

If you do have $50 at start and end up with $40, is that a "win $40" or "lose $10"?

Perhaps it would be more clear to start with some named values:
1
2
3
4
int main() {
  const int Bet = 10;
  const int Initial = 50;
  int cash = Initial;

Now we could use Bet rather than mysteroious 10 in our code.

I did ask whether heads+tails==flips. It should.
If it does, then by rearraging equation we get: tails = flips - heads;
There are thus two ways for the loop:
1. Keep track of heads and tails and then calculate flips
2. Keep track of heads(or tails) and flips and then calculate tails(or heads)
Topic archived. No new replies allowed.