Using a while loop write a simple coin flip game.

Hello can you help me change the code so that it functions properly.

It keeps saying sorry you lose. the coin came up o.

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

using namespace std;

int main()
{

	char answer;
	double bankTotal = 10;
	int zeroOne;
	char headsortails;
	int Heads = 0;
	int Tails = 1;



	srand(static_cast<unsigned int>(time(0)));
	zeroOne = rand() % 2;


	cout << "Welcome to the coin flip game. It cost 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 >> answer;


	while (toupper(answer) == 'Y')
	{

		cout << "Your bank is $" << bankTotal << endl;
		cout << "Enter heads or tails (h/t)" << endl;
		cin >> headsortails;

		while (headsortails == zeroOne)
		{
			cout << "Winner, the coin came up " << zeroOne << endl;
			bankTotal = 10 + 2;
		}
		while (headsortails != zeroOne)
		{
			cout << "Sorry, you loose. The coin flip came up " << zeroOne << endl;
			bankTotal = 10 - 1;
		}


		cout << "Would you like to play again (y/n)? " << endl;


	}

	cout << "Thanks for playing, your bank is $" << bankTotal << endl;
	cout << "Please come again " << endl;

	return 0;
}
Do you know the difference between if and while?
So replace while with if but which one?
try replacing all the while with if and the third while should be else. just suggesting
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
56
#include <iostream>
#include <ctime>

using namespace std;

int main()
{

	char answer;
	double bankTotal = 10;
	int zeroOne;
	char headsortails;
	int Heads = 0;
	int Tails = 1;



	srand(static_cast<unsigned int>(time(0)));
	zeroOne = rand() % 2;


	cout << "Welcome to the coin flip game. It cost 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 >> answer;


	while (toupper(answer) == 'Y')
	{

		cout << "Your bank is $" << bankTotal << endl;
		cout << "Enter heads or tails (h/t)" << endl;
		cin >> headsortails;

		if (headsortails == zeroOne)
		{
			cout << "Winner, the coin came up " << zeroOne << endl;
			bankTotal = 10 + 2;
		}
		if (headsortails != zeroOne)
		{
			cout << "Sorry, you loose. The coin flip came up " << zeroOne << endl;
			bankTotal = 10 - 1;
		}


		cout << "Would you like to play again (y/n)? " << endl;


	}

	cout << "Thanks for playing, your bank is $" << bankTotal << endl;
	cout << "Please come again " << endl;

	return 0;
}


This is what I have still cant figure it out.
What do you think this line if (headsortails == zeroOne) is doing? zeroOne is going to be either 0 or 1. headsortails will be whatever character the user entered at the prompt. It looks like you're suggesting 'h' or 't', but even if the user put in '0' or '1' the condition would fail anyway. '0' is not the same as 0. One is a character, the other is a number. Think about the input you're getting from the user and what you want to compare it to.
Topic archived. No new replies allowed.