Skip to content

Repository files navigation

Number Guessing Game (C++)

The Number Guessing Game is a simple C++ console application where the computer generates a random number between 1 and 100, and the player attempts to guess it. After each guess, the program provides feedback indicating whether the guess is too high or too low until the correct number is guessed. It also keeps track of the total number of attempts.

Technologies Used

  • C++
  • Visual Studio Code
  • C++ Standard Library (iostream)
  • C++ Standard Library (cstdlib)
  • C++ Standard Library (ctime)

Concepts Used

  • Random Number Generation – Used rand() to generate a random number and srand(time(NULL)) to ensure a different number is generated each time the program runs.
  • Loops – Used a do-while loop to repeatedly ask the user for guesses until the correct number is entered.
  • Conditional Statements – Used if, else if, and else statements to compare the user's guess with the generated number.
  • Variables – Used variables to store the random number, the user's guess, and the number of attempts.
  • Arithmetic Operators – Incremented the number of tries using the ++ operator.
  • User Input and Output – Used cin for accepting guesses and cout for displaying feedback.

The Process

  • Generate a random number between 1 and 100.
  • Display the game title and instructions.
  • Ask the user to guess the number.
  • Compare the user's guess with the generated number.
  • Display "Too high!" if the guess is greater than the number.
  • Display "Too low!" if the guess is less than the number.
  • Repeat the process until the correct number is guessed.
  • Display a congratulatory message and the total number of attempts.

What I Learned

  • How to generate random numbers in C++.
  • How to use srand() and time() to create different random values each time the program runs.
  • How to use do-while loops for repeated user interaction.
  • How to compare values using conditional statements.
  • How to count user attempts using variables.
  • How to build a simple interactive console game.

How It Could Be Improved

  • Add difficulty levels (Easy, Medium, Hard) with different number ranges.
  • Limit the number of guesses and display a game over message if the player runs out of attempts.
  • Validate user input to prevent invalid entries.
  • Allow the player to play multiple rounds without restarting the program.
  • Provide hints, such as how close the guess is to the correct number.
  • Keep track of the player's best score (fewest guesses).

How to Run the Project

  • Step 1: Open the project in Visual Studio Code or any C++ IDE.
  • Step 2: Compile the program using a C++ compiler.
  • Step 3: Run the program.
  • Step 4: Enter a number between 1 and 100 when prompted.
  • Step 5: Continue guessing until you find the correct number.
  • Step 6: View the total number of attempts after winning the game.

Sample Output

image alt image alt

About

A simple C++ Number Guessing Game where the computer randomly generates a number, and the player must guess it. The program provides hints whether the guess is too high or too low until the correct number is guessed.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages