Create an application using the following names for the solu

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 VB2015\\Chap05 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.

Solution

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;
}

Create an application, using the following names for the solution and project, respectively: Guessing Game Solution and Guessing Game Project. Save the applicat

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site