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

Create an application, using the following names for the solution and project, r

ID: 3934743 • Letter: C

Question

Create an application, using the following names for the solution and project, respectively: Guessing Game Solution and Guessing Game Project. Save the application in the VB2015Chap05 folder. The application should generate a random integer from 1 through 30, inclusive. It then should give the user as many chances as necessary to guess the integer. Each time the user makes a guess, the application should display one of three messages: “Guess higher”, “Guess lower”, or “Correct. The random integer is x.”, where x is the random integer. The application should also display the number of chances that were required for the user to guess the number. Create a suitable interface, and then code the application. Test the application appropriately.

Explanation / Answer

Following is the C++ code for the application :

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(){

   srand( time(NULL) );
   int guess;
   int totalChancesTaken = 0;
   int   randomNumber = rand()%30 + 1; //random number between 1 and 30
  
   while( true ){
       totalChancesTaken++;
       cout<< "Your Guess: ";
       cin >> guess;
       if( guess > randomNumber ){
           cout << "Guess Lower" << endl;
       }
       else if( guess < randomNumber ){
           cout << "Guess Higher" << endl;
       }
       else{
           cout << "Correct. The random integer is " << randomNumber << "." << endl;
           break;
       }
   }  
   cout << "Total chances taken : " << totalChancesTaken << 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