I know there are answers on Chegg already, but none seem to run or fit the assin
ID: 3723424 • Letter: I
Question
I know there are answers on Chegg already, but none seem to run or fit the assingment.
A simple game of chance
Using a while loop write a simple coin flip game in C++.
Specifics:
The user starts with a bank of $10.00
It costs a dollar to play
A correct guess pays $2.00
The while loop is used for the game loop. It should ask if the user wants to play.
The while loop will check for a value of 'Y' or 'N'. You should also allow for lower case letters to be input.
You MUST prime the while loop by asking if the user wants to play before the loop starts.
You should output the bank before asking for a guess.
You should use the random number generator to generate a value of 0 and 1. A value of 0 = Heads (H), and a value of 1 = Tails (T);
Your program will take in a value of 'h' (Heads) or 't' (Tails) as the input value of the guess.
To determine if the user wins you will compare the users guess to the value generated by the random number generator.
Note that a conversion will have to take place to convert the number generated to a character.
The game should continue until the user indicates they do not want to play anymore. Your code should ask if the user wants to play again.
When the while loop ends you should thank the user for playing and output the bank.
A do-while loop is not a while loop. You MUST use a while loop.
Explanation / Answer
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int bank_bal = 10;
int correct_reward = 2;
int cost_of_play = 1;
char choice = 'Y';
char user_choice;
srand (time(NULL));
cout<<"Do you want to play? (Y/N): ";
cin>>choice;
while(choice == 'Y' || choice == 'y'){
bank_bal-=1;
cout<<"Balance left: "<<bank_bal<<endl;
cout<<"Enter choice (h/t) :";
cin>>user_choice;
int random_num = rand()%1;
char random_char = (random_num==0)?'h':'t';
if(random_char == user_choice || random_char == user_choice+32){
bank_bal+=2;
}
cout<<"Do you want to play? (Y/N): ";
cin>>choice;
}
cout<<"Thank you for playing. Balance: "<<bank_bal;
return 0;
}