Coin Flip using while loop C++ Help

Hey guys,

I am completely lost after writing a tiny portion of the code.

There are the specifics:
The user starts with a bank of $10.00
It costs a dollar to play
A correct guess pays $2.00
The while loop is used for the game loop. It should ask if the user wants to play.
The while loop will check for a value of 'Y' or 'N'. You should also allow for lower case letters to be input.
You MUST prime the while loop by asking if the user wants to play before the loop starts.
You should output the bank before asking for a guess.
You should use the random number generator to generate a value of 0 and 1. A value of 0 = Heads (H), and a value of 1 = Tails (T);
Your program will take in a value of 'h' (Heads) or 't' (Tails) as the input value of the guess.
To determine if the user wins you will compare the users guess to the value generated by the random number generator.
Note that a conversion will have to take place to convert the number generated to a character.
The game should continue until the user indicates they do not want to play anymore. Your code should ask if the user wants to play again.
When the while loop ends you should thank the user for playing and output the bank.
A do-while loop is not a while loop. You MUST use a while loop.


The output looks something like this:
Welcome to the coin flip game. It costs a dollar to play.
If you guess correctly, you will win $2.00.
Do you want to play (y/n)?
y
Your bank is $10
Enter heads or tails (h/t)
h
Winner, the coin flip came up heads
Would you like to play again (y/n)?
y
Your bank is $11
Enter heads or tails (h/t)
t
Winner, the coin flip came up tails
Would you like to play again (y/n)?
y
Your bank is $12
Enter heads or tails (h/t)
t
Sorry, you lose. The coin flip came up heads
Would you like to play again (y/n)?
n
Thanks for playing, your bank is $11
Please come again.

I would appreciate any help to this problem. I am pretty new to the language and I think that my class is moving ahead really fast for me to handle as a beginner.

Thanks!

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
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
	char ser;

	cout << "Welcome to the coin flip game. It costs a dollar to play." << endl;
	cout << "If you guess correctly, you will win $2.00" << endl;
	cout << "Do you want to play (y/n)?" << endl;
	cin >> ser;

	int guess;
	int answer;
	int bank = 10, h = 0, t = 1;

	while ()
	{
		cout << "Your bank is $10" << endl;
		cout << "Enter heads or tails (h/t)" << endl;
		cin >> guess;
	}


	return 0;
}
While loop should have the condition if the ser is not 'y' the loop ends.
line 21: cout << "Your back account is $" << bank << endl; so you can keep track of the money in your bank.

You need srand seed and rand. Your guess needs to be a char for 'h' or 't'
This is what I have changed so far:
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
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
	char ser;

	cout << "Welcome to the coin flip game. It costs a dollar to play." << endl;
	cout << "If you guess correctly, you will win $2.00" << endl;
	cout << "Do you want to play (y/n)?" << endl;
	cin >> ser;

	srand(time(0));

	char guess;
	int num = rand() % 2;
	int bank = 10, heads = 0, tails = 1;

	while (ser == 'y')
	{
		cout << "Your bank is $" << bank << endl;
		cout << "Enter heads or tails (h/t)" << endl;
		cin >> guess;

		num = rand() % 2;
		
		if (num == heads)
		{
			cout << "Winner, the coin flip came up heads" << endl;
			cout << "Do you want to play again (y/n)?" << endl;
			cin >> ser;
		}
		if (num == tails)
		{
			cout << "Sorry, you lose. The coin flip came up tails" << endl;
			cout << "Do you want to play again (y/n)?" << endl;
			cin >> ser;
		}

		if (guess != num)
		{
			bank++;
			cout << "Your bank is $" << bank << endl;
		}
		else if (guess == num)
		{
			bank--;
			cout << "Your bank is $" << bank - 0.00 << endl;
		}
	}

	return 0;
}


I am missing the code for when the user wins, they get $2.00, but when they lose, their bank gets deducted by $1.00. How do I go about doing that. Thanks for any help. I will gladly appreciate it!
Last edited on
Topic archived. No new replies allowed.