Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A simple game of chance In C++, Using a while loop write a simple coin flip game

ID: 3722359 • Letter: A

Question

A simple game of chance

In C++, Using a while loop write a simple coin flip game.

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.h>

Using namespace std;

Int main()

{

                Int balance=10;

               Char choice,ch,B,option;

                Cout<<”Do you want to play(Y/N)”<<endl;

               Cin>>choice;

                

                While(choice == 'Y' || choice == 'y')

                {

                       Cout<<”your balance is ”<<balance<<endl;

                       Cout<<”Enter head or tail(H/T)”<<endl;

                      Cin>>ch;

                     Balance=balance-1;

                      Int a =rand() %2;

                      If(a=1)

                                B=’H’;

                      Else

                                B=’T’;

                      If(ch==B)

                                {

                                Cout<< “You win”;

                             Balance=balance+2;

                                }

else                       

                                {

                                Cout<< “You loose”;

                                }

                                Cout<<”Do you want to play again(Y/N)”;

                             Cin>>option;

                                If(option=y)

                                                Contine;

                                Else

                                                Cout<<”Thank you for playing”;

                                                Break;

                      }

                Cout<<”wrong choice”;                                

                }

            

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote