Need help with a coin flip game and winnings

I need to make a coin flip game where you start with 10 dollars.
It costs a dollar to play
a correct guess adds two dollars to your bank.
I am stuck with adding winnings to the bank.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
  #include <iostream>
#include <ctime>

using namespace std;

int main()
{
	char choice;

	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 >> choice;

	srand(time(0));

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

	//Variables
	winnings = 2.00;
	bank = bank + winnings;

	while (choice == 'y' || 'Y')
	{
		cout << "Your bank is $" << bank << endl;
		cout << "Enter heads or tails (H/T)" << endl;
		cin >> guess;

		num = rand() % 2;

		if (guess == 'h' || 'H')
		{
			cout << "Winner, the coin flip came up heads" << endl;
			cout << "Do you want to play again (Y/N)?" << endl;
			cin >> choice;
		}
		if (guess == 'h' || 'H')
		{
			cout << "Sorry, you lose. The coin flip came up tails" << endl;
			cout << "Do you want to play again (Y/N)?" << endl;
			cin >> choice;
		}
		if (guess == 't' || 'T')
		{
			cout << "Winner, the coin flip came up tails" << endl;
			cout << "Do you want to play again (Y/N)?" << endl;
			cin >> choice;
		}
		if (guess == 't' || 'T')
		{
			cout << "Sorry, you lose. The coin flip came up heads" << endl;
			cout << "Do you want to play again (Y/N)?" << endl;
			cin >> choice;
		}
		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;
}
closed account (48T7M4Gy)
This might throw a bit of light on it:
1. toupper overcomes your if error - if you stick to upper and lower case tests then you need if(choice == 't' || choice == 'T') etc. It's better to convert to upper case I think.

2. If the logic is simply guessing the computer toss then the test and reward/penalty loop is fairly simple.

3. It's a good idea to plan your code with some sort of flow chart to make sure you understand how the sequence of calculations run in the loop structure.

4. You still have to consider whether the request to continue should be inside or outside the loop. If you run the program and only ask the question once at the start it's a fairly redundant sort of question to ask :)

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

using namespace std;

int main()
{
    char choice;
    
    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)? ";
    cin >> choice;
    
    char computer_choice[] {'H', 'T'};
    
    srand (time_t(NULL));
    
    char guess;
    int num = rand() % 2;
    
    double bank = 10.00;
    int winnings = 0.0;
    
    while (toupper(choice) == 'Y')
    {
        cout << "Your bank is $" << bank << endl;
        
        cout << "Enter heads or tails (H/T) ";
        cin >> guess;
        guess = toupper(guess);
        num = rand() % 2;
        
        char computer_throw = computer_choice[num];
        cout << "Computer throws " << computer_throw << '\n';
        
        if (guess == computer_throw)
        {
            cout << "You win\n";
            winnings = 2.00;
        }
        else
        {
            cout << "You lost\n";
            winnings = -2.00;
        }
        
        num = rand() % 2;
        
        bank += winnings;
    }
    
    return 0;
}
Topic archived. No new replies allowed.