How do I convert random number generator to equal a character?

For this project, I am creating a coin flip game. It will ask the user to input Heads (H) or Tails (T). If they guess correctly they will win $2. If incorrect -$1. I am having trouble with the random number generator. I want to convert the numbers it outputs to either 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
#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;
}
Last edited on
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

int main()
{
   srand(time(nullptr));
   int coin_flip = 0;
   std::string coin_name[2] = { "Heads", "Tails" };

   for (int i = 0; i < 100; i++)
   {
      coin_flip = (rand() % 2);
      std::cout << coin_name[coin_flip] << "  ";
   }

   return 0;
}
Topic archived. No new replies allowed.