While Looping

I have been trying to figure out what is wrong with this code but I cannot. I am new to C++, so maybe that's the reason. I am not here for an answer, just give me a hint, please. Thank You.
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
  #include <iostream>
#include <ctime>

using namespace std;

int main()
{
	char HorT;

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

	srand((unsigned int)time(0));

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

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

		num = rand() % 2;

		if (num == 0)
		{
			cout << "Winner, the coin flip came up heads" << endl;
			cout << "Do you want to play again (y/n)?" << endl;
			cin >> HorT;
			cout << "Your bank is $" << bank++ << endl;
		}
		else
		{
			cout << "Sorry, you lose. The coin flip came up heads" << endl;
			cout << "Do you want to play again (y/n)?" << endl;
			cin >> HorT;
			cout << "Your bank is $" << bank - 1 << endl;
		}
	   if (num == 1)
		{
			cout << "Winner, the coin flip came up tails" << endl;
			cout << "Do you want to play again (y/n)?" << endl;
			cin >> HorT;
			cout << "Your bank is $" << bank++ << endl;
		}
		
		if (HorT == tolower('n'))
		{
			cout << "Thanks for playing, your bank is $" << bank << endl;
			cout << "Please come again " << endl;
		}



	}
	return 0;
}
What help do you need ?
Does your program compile?
If not, what are the errors?
If it does compile, does the program not run as expected?
Tell us what happens and what is supposed to happen
This code is not compiling like it should. I mean right now sometimes it outputs wrong notes with heads and tails and it outputs the bank amount in dollars statement twice. I now there is something wrong but I just cannot catch the problem. Thank You.
Last edited on
You seem sure that there is something wrong. What do you see as wrong?
* The code fails to compile?
* The program crashes?
* The output is not what it should? How does it differ from expected?

Why is 51-55 inside the loop?

You have line 23. Do you need lines 34, 41, 48?

If line 29 is false, then both 38-41 AND 45-48 are executed.

You never use the guess.

Line 41 does not modify the bank.

Line 51 computes the lower case form of 'n', which is ... 'n'. Why?
This program should use a while loop to make a coin flip program. The program will ask the user that if the user wants to play the game 'Y' for yes and 'N' for no. The cost for playing the game is 1 dollar and the bank starts at $10. If you win you get $2 and if you loose, you loose that $1 you used to play. For the game, you have to guess 'H' for heads and 'T' for tails, if you get it right the computer should output "Winner, the coin flip came up ______" and if you guess the wrong answer, the system will output a message telling you the right answer. Hope that makes sense. Thank You
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/218505/
I would structure the program like this:
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
int main()
{
  bool running = true;
  char want_to_play = 0, player_choice = 0, computer_choice = 0;
  int bank = 10;

  srand(time(nullptr));
  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.get(want_to_play);
  cin.ignore(256, '\n');
  running == toupper(want_to_play);

  while (running)
  {
    cout << "Your bank is $" << bank << endl;
    cout << "Enter heads or tails (h/t)" << endl;
    cin.get(player_choice);
    cin.ignore(256, '\n');
    computer_choice = rand() % 2;
    if (computer_choice == player_choice)
    {
      // handle player win
    }
    else
    {
      // handle player loss
    }
    if (bank == 0) // player broke
    {
      // display game over
      running = false;
    }
    else
    {
      cout << "Do you want to play again (y/n)?" << endl;
      cin.get(want_to_play);
      cin.ignore(256, '\n');
      running = toupper(want_to_play) == 'Y';
    }
  }
  system("pause");
  return 0;
}
closed account (48T7M4Gy)
I can't remember why I didn't post the not enough money part before but anyway ... thanks for the reminder:

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
#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 = 0;//rand() % 2;
    
    double bank = 10.00;
    double cost_to_play = 1.00;
    double winnings = 0.0;
    
    while (toupper(choice) == 'Y')
    {
        cout << "Your bank is $" << bank << ", ";
        
        if(bank >= 3)
        {
            bank -= cost_to_play;
            cout << "you have enough money to play\n";
            
            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;
            }
        }
        else
        {
            cout << "You don't have enough to play\n";
            break;
        }
        
       // num = rand() % 2;
        
        bank += winnings;
    }
    
    return 0;
}
Last edited on
Thank You, everyone!! I appreciate the help!!.
Topic archived. No new replies allowed.