C++

C++ program for a simple High-Low guessing game

Here’s a C++ program for a simple High-Low guessing game.

In this game, the computer randomly generates a number between 1 and 100, and the player has to guess it.

The computer will give hints after each guess, telling the player if their guess is too high, too low, or correct.

Code

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

int main() {
    // Seed random number generator
    std::srand(std::time(0));

    // Generate random number between 1 and 100
    int target_number = std::rand() % 100 + 1;
    int guess;
    int attempts = 0;

    std::cout << "Welcome to the High-Low Guessing Game!" << std::endl;
    std::cout << "I'm thinking of a number between 1 and 100." << std::endl;

    // Main game loop
    while (true) {
        std::cout << "Enter your guess: ";
        std::cin >> guess;
        attempts++;

        if (guess < target_number) {
            std::cout << "Too low. Try again!" << std::endl;
        } else if (guess > target_number) {
            std::cout << "Too high. Try again!" << std::endl;
        } else {
            std::cout << "Congratulations! You guessed the number " << target_number << " in " << attempts << " attempts." << std::endl;
            break;
        }
    }

    return 0;
}

Explanation

  1. Include Required Libraries:
    • #include <iostream>: Allows for input and output operations (e.g., std::cin and std::cout).
    • #include <cstdlib>: Provides the std::rand() function to generate random numbers.
    • #include <ctime>: Provides std::time() for seeding the random number generator.
  2. Seed the Random Number Generator:
    • std::srand(std::time(0));: Seeds the random number generator with the current time. This ensures that each time the program runs, it generates a different random number.
  3. Generate the Target Number:
    • int target_number = std::rand() % 100 + 1;: Generates a random number between 1 and 100.
      • std::rand() % 100 produces a number between 0 and 99.
      • Adding 1 shifts the range to 1–100.
  4. Initialize Variables:
    • int guess;: Holds the player’s current guess.
    • int attempts = 0;: Tracks the number of attempts the player has made.
  5. Welcome Message:
    • std::cout statements display a welcome message and instructions for the player.
  6. Main Game Loop (while (true)):
    • This loop continues until the player guesses the correct number, at which point the break statement exits the loop.
    • Prompt the Player for a Guess:
      • std::cout << “Enter your guess: “;: Prompts the player to enter a guess.
      • std::cin >> guess;: Reads the player’s guess from the console.
      • attempts++;: Increments the attempt count.
    • Check the Player’s Guess:
      • if (guess < target_number): If the guess is less than the target number, the program outputs “Too low” and continues.
      • else if (guess > target_number): If the guess is greater than the target number, the program outputs “Too high” and continues.
      • else: If the guess matches the target number, the player has won:
        • The program displays a congratulatory message and the number of attempts.
        • The break statement exits the loop, ending the game.
  7. Return Statement:
    • return 0;: Ends the program successfully.

Sample Gameplay

If the target number is 37, a sample game might look like this:

Welcome to the High-Low Guessing Game!
I'm thinking of a number between 1 and 100.
Enter your guess: 50
Too high. Try again!
Enter your guess: 25
Too low. Try again!
Enter your guess: 37
Congratulations! You guessed the number 37 in 3 attempts.

Notes

  • Randomness: The call to std::srand(std::time(0)); ensures that each game session generates a new random number.
  • Error Handling: This basic implementation does not handle non-integer input, so the player should only enter integers.
  • Attempts Tracking: The program tracks the number of attempts, which it displays when the player correctly guesses the number.

Summary

This C++ High-Low Guessing Game is a simple example of using loops, conditionals, and random number generation.

The program generates a random target number and gives the player hints after each guess, providing an interactive way to practice basic C++ concepts.

Related posts

C++ program that simulates a Magic 8 Ball

A C++ program that displays a random quote