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

After printing out the win/loss messages, ask the user whether they want to play

ID: 3866153 • Letter: A

Question

After printing out the win/loss messages, ask the user whether they want to play again and have them answer 'y' or 'n'. When the user quits with the 'n' answer, the program should output the total number of wins and losses. If they answer 'y', then a new random number should be generated and they will of course, have 5 attempts again to guess the number.

Here's some pseudo code that describes the outerloop of game control.

set answer to 'y'

while the answer is 'y' do the following steps

   generate the random number

keep asking for a guess and comparing the guess to the computer's number until they win or have exceeded 5 guesses

print different messages out depending on whether they've won or lost

ask if they want to continue and set the response to the variable "answer"

They don't want to continue so print out the win/loss statistics

#include #include #include using namespace std; int main () int x; srand (time (NULL)) int y rand() % 20 +1; // this code will generate our random number //cout

Explanation / Answer

#include<iostream>
#include<cstdlib>
#include<time.h>
#include<string>

using namespace std;

int main()
{
   int x;
   int y;
   char choice;
   int count_attempt;
   int count_wins;
   int count_losses;
   string win[3];
   string lose[3];
   string ask[2];

   win[0] = "Great!! you won!!! ";
   win[1] = "Congratulation!!! you are winner ";
   win[2] = "Wow!! Your guess is wright!! ";

   lose[0] = "Better luck next time ";
   lose[1] = "Ohh!!! you lost ";
  

   ask[0] = "Would like to try again ";
   ask[1] = "Want to play again!!! ";


   srand(time(NULL));
  

   cout << "Can you guess the next number the program will generate next ";
   cout << "Hint, it will generate a random number from 1 to 20!!! ";
   count_wins = 0;
   count_losses = 0;
   do{
        count_attempt=0;
        y = rand()%20 + 1;       
        cout << "Please enter your guess:";
        do {
              cin >> x;
              if (x == y) {
                 cout << win[rand()%3];
                 count_wins++;
              }
              else {
                 count_attempt++;
                 if (count_attempt < 5) {
                    cout << "Sorry:(Your guess was Wrong!! ";
                    cout << "Please try again" << endl;
                 }
                 else {
                    cout << lose[rand()%2] << endl;
                    count_losses++;
                 }
              }
         } while (x != y && count_attempt<5);
         cout << ask[rand()%2] << endl;
         cout << "Enter your choice(y/n):" << endl;
         cin >> choice;
   } while (choice != 'n');
   cout << "Game Stats" << endl;
   cout << "Number of wins:" << count_wins << endl;
   cout << "Number of losses:" << count_losses << endl;  
   return 0;
}

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