Need Help converting a variable

My program is almost finished but has a slight mishap that I need to fix but don't know how to. Variable coin_flip produces a 1 or 0, but i need to convert them to output 'Heads' or 'Tails'.

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

using namespace std;

int main()
{

	char answer;
	double bankTotal = 10;
	int coin_flip;
	char headsortails;

	srand(static_cast<unsigned int>(time(0)));
	coin_flip = 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 (toupper(headsortails == coin_flip))
		{

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


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


	}

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


	return 0;
}
Last edited on
1
2
3
4
std::string result(int toss) 
{
	return toss ? "heads" : "tails";
}
could you show me how I would be able to incorporate it into my code?
http://www.cplusplus.com/doc/tutorial/functions/

why are you using while loops?
i think instead of this:
while (toupper(headsortails == coin_flip))

you want something like this:

1
2
3
4
5
6
7
8
if(headsortails == 'h')
{
...
}
else
{
...
}
Last edited on
For this program I wanted the system to randomly generate either 'Heads or Tails' and the user had to guess. From what I understand if I use

 
if(headsortails == 'h')


wouldn't the user only win if they input 'h' or vice versa?
let me populate some more of what i posted as a hint:

1
2
3
4
5
6
7
8
9
10
11
12

if(headsortails == 'h')
{
cout << "Winner, the coin came up " << coin_flip << endl;
			bankTotal = bankTotal + 2;
}
else
{
cout << "Sorry, you loose. The coin flip came up " << coin_flip << endl;
			bankTotal = bankTotal - 1;
}
Last edited on
Topic archived. No new replies allowed.