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.
#include <iostream>
#include <ctime>
usingnamespace 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;
}
elseif (guess == num)
{
bank--;
cout << "Your bank is $" << bank - 0.00 << endl;
}
}
return 0;
}
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 :)