How to resolve the algorithm Guess the number/With feedback step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Guess the number/With feedback step by step in the C++ programming language

Table of Contents

Problem Statement

Write a game (computer program) that follows the following rules:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Guess the number/With feedback step by step in the C++ programming language

Goal of the Code:

This C++ code simulates a simple number guessing game where the user tries to guess a random number generated by the computer.

Breakdown of the Code:

1. Header Includes:

#include <iostream>
#include <cstdlib>
#include <ctime>
  • <iostream>: For input/output operations.
  • <cstdlib>: For the rand() function to generate random numbers.
  • <ctime>: To seed the random number generator using the current time.

2. Main Function:

int main()
  • The main() function is the entry point of the program.

3. Seed the Random Number Generator:

std::srand(std::time(0));
  • This line seeds the random number generator using the current time, ensuring a different random sequence every time the program is run.

4. Initialize Variables:

  • lower: Lower limit of the guessing range (input by the user).
  • upper: Upper limit of the guessing range (input by the user).
  • guess: User's guess.
  • random_number: Random number generated by the computer.

5. Get User Input for Range Limits:

std::cout << "Enter lower limit: ";
std::cin >> lower;
std::cout << "Enter upper limit: ";
std::cin >> upper;
  • This prompts the user to input the lower and upper limits of the guessing range.

6. Generate Random Number:

int random_number = lower + std::rand() % ((upper + 1) - lower);
  • This line generates a random number within the specified range [lower, upper].

7. Start the Guessing Game Loop:

do
{
   // Prompt the user to guess.
   std::cout << "Guess what number I have: ";
   std::cin >> guess;

   // Check if the guess is too high or too low.
   if (guess > random_number)
       std::cout << "Your guess is too high\n";
   else if (guess < random_number)
       std::cout << "Your guess is too low\n";
   else
       std::cout << "You got it!\n";
} while (guess != random_number);
  • This loop continues until the user guesses the correct random number (guess == random_number).

8. Exit the Program:

return 0;
  • This line indicates successful program execution and exits the program.

Source code in the cpp programming language

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    std::srand(std::time(0));
    int lower, upper, guess;
    std::cout << "Enter lower limit: ";
    std::cin >> lower;
    std::cout << "Enter upper limit: ";
    std::cin >> upper;
    int random_number = lower + std::rand() % ((upper + 1) - lower);

    do
    {
        std::cout << "Guess what number I have: ";
        std::cin >> guess;
        if (guess > random_number)
            std::cout << "Your guess is too high\n";
        else if (guess < random_number)
            std::cout << "Your guess is too low\n";
        else
            std::cout << "You got it!\n";
    } while (guess != random_number);

    return 0;
}


  

You may also check:How to resolve the algorithm Unicode variable names step by step in the Ol programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Swift programming language
You may also check:How to resolve the algorithm Define a primitive data type step by step in the C# programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the MoonScript programming language
You may also check:How to resolve the algorithm Subleq step by step in the Oforth programming language