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.
#include <iostream>
#include <ctime>
usingnamespace 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((unsignedint)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.
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
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;
}