CS116 C In this assignment you will implement a number guess

 CS-116 C++  In this assignment you will implement a number guessing game.  The program will start by asking the user for the maximum number for a range. The program will then tell the user the most number of tries it should take to guess a random number between 1 and the maximum value entered (an explanation of how to calculate this is provided below).  The program will then pick a random number between 1 and the maximum number specified by the user.  The user will continuously try to guess the number, and the program will inform the user if the guesses are too high or too low and will indicate if the user is getting close.  Note, \"close\" is defined as within + or - 10% of the max value value.  For example, if the max value is 50 then the user is close if they are within 5 of the correct value.  If the user guesses the correct number, the program should tell the user how many guesses it took, and then ask the user if they want to play again.  Here\'s a sample of what the program run may look like:    What\'s the maximum random number value: 50    You can do it in 6 guesses or less.    Enter your guess: 25   Too low.    Enter your guess: 37   Too low.    Enter your guess: 43   Too low, but you\'re real close!    Enter your guess: 46   Too high, but you\'re real close!    Enter your guess: 44   Correct. You got it in 5 guesses.    Do you want to play again (y/n): n   Thanks for playing.  You program should have nicely formatted input and output similar to what is shown above.  The FAQ for this assignment has information on how to generate a random number.  There is also a sample program provided called rand.cpp that illustates how to generate random numbers.  The most number of guesses it should take to guess a value in a range of numbers can be calculated by determining the power of 2 that is closest to, but not less than, the max value in the range.  Here\'s some examples:    If the max value is 100, the closest power of 2 is 128   or 2^7, so the most guesses it should take is 7.    If the max value is 50, the closest power of 2 is 64,   or 2^6, so the most guesses it should take is 6.    If the max value is 10, the closest power of 2 is 16   or 2^4, so the most guesses it should take is 4.  Things I\'ll be looking for in this assignment:    - Everything I was looking for in assignment #1    - good use of conditionals and looping.  Recursion     (a function calling itself ... directly or     indirectly) is NOT allowed in this assignment.    - breaking down your code into numerous small,     functions to alievate multiple nested blocks and     separate functionality into logical small units.    - no use of global variables.  All variables should     be local variables.  If data needs to be used     by more than one function it should be passed     as a function parameter or return value. 

Solution

#include <iostream> // library that contains basic input output functions

#include <cstdlib> // library that contains random numbers generation functions

#include <time.h> // library that contains time functions.

#include <string> // library to use advanced c++ strings

using namespace std;

int main()

{

    bool play_again = true;

    while(play_again == true) //loop will run on user\'s entered choice to play game again or not

    {

      /* setting time as feed to generate random number.it help us generate different random depend upon

      computer\'s current time.time(NULL) provides current time stamp*/

      srand(time(NULL));

      int number = rand() % 100 + 1; //generating and storing and random number between 1-100

                /* while loop will be terminated on this flag. When user will enter

      correct number as generrated by computer, this flag will be set to true*/

      bool is_guess_correct = false;

                int input_number; //will be used to save user input number

                int attempts_count = 1; // keep track of attempts user has made to guess correct number

   

      //this loop will take number again and again until user enters correct number

                while(is_guess_correct == false)

                {

          //Displaying different prompt lines for first and all other attempts

                                  if(attempts_count == 1) // if it is first attempt to guess the number then don\'t print \"Again\"

                                  {

                                                                cout << \"Enter Number : \";

                                  }

                                  else

                                  {

                                                                cout << \"Enter Number Again : \"; //this line will be printed on all attempts except first one

                                  }

                                  cin >> input_number; // taking number as input in input_number

         

          /*Check if input number is same as that of randomly generated number or not*/

                                  if(input_number == number)

                                  {

            //We are here because user entered same number as generated by computer

                                                                cout << \"Congratulation! You have guessed the correct number in \" << attempts_count << \" attempts\" << endl;

                                                                is_guess_correct = true; //settting this flag to true because we want to terminate loop

                                  }

                                  else

                                  {

            //We are here because user did not entered same number as generated by computer

                                                                attempts_count++; // increamenting attempts

             /*Displaying informational message that helps user to determine number quickly*/

                                                                if(input_number < number) //if user entered number is less than random number

                                                                {

                                                                   cout << \"Entered number is small than then number to be guess.\" << endl;

                                                                }

                                                                else //if user entered number is greater than random number

                                                                {

                                                                   cout << \"Entered number is greater than then number to be guess.\" << endl;

                                                                }

                                  }

                }

      //Asking user if he/she want to continue game or not

      string choice;

      cout << \"Press Y to play again or any other key to terminate : \";

      cin >> choice;

      //if user enters any other key than Y/y, set play_again flag to false

      if(choice != \"Y\" && choice != \"y\")

      {

        play_again = false;

      }

                }

   return 0; //Successful teermination of program returns zero

}

 CS-116 C++ In this assignment you will implement a number guessing game. The program will start by asking the user for the maximum number for a range. The prog
 CS-116 C++ In this assignment you will implement a number guessing game. The program will start by asking the user for the maximum number for a range. The prog
 CS-116 C++ In this assignment you will implement a number guessing game. The program will start by asking the user for the maximum number for a range. The prog

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site