C++ Number Guessing Game Project
The computer picks a secret number; you hunt it down with higher/lower hints.
What you will build
The classic first game: the program picks a random number between 1 and 100 and gives you higher-or-lower feedback after every guess. It teaches the game loop pattern — repeat until a win condition — that nearly every interactive program you'll ever write is built on.
What you will learn
- Generating random numbers with <random>
- Looping until a condition is met
- Giving feedback based on comparisons
- Tracking and limiting attempts
Feature checklist
- Random secret number in a chosen range
- Higher / lower hints after each guess
- Attempt counter and a win message
- Play-again prompt
- Optional difficulty levels (range or max attempts)
Starter code
#include <iostream>
#include <random>
int main() {
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> dist(1, 100);
int secret = dist(rng);
int guess = 0, attempts = 0;
std::cout << "I'm thinking of a number from 1 to 100.\n";
while (guess != secret) {
std::cout << "Your guess: ";
if (!(std::cin >> guess)) return 0;
++attempts;
if (guess < secret) std::cout << "Too low!\n";
else if (guess > secret) std::cout << "Too high!\n";
}
std::cout << "Correct in " << attempts << " attempts!\n";
}Full walkthrough
Step 1: Set up the file and the random engine
Include <iostream> for I/O and <random> for proper random numbers. The starter builds a std::mt19937 engine seeded from std::random_device, then a std::uniform_int_distribution<int> dist(1, 100) — the distribution is what guarantees every number from 1 to 100 is equally likely, which rand() % 100 does not.
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> dist(1, 100);
int secret = dist(rng);Step 2: Declare the game state
You need three ints: secret (the target), guess (starts at 0 so it cannot accidentally equal the secret), and attempts (starts at 0). Print the invitation line 'I'm thinking of a number from 1 to 100.' before the loop so the player knows the range — the range is a promise the hints rely on.
Step 3: Write the game loop
The whole game is while (guess != secret). Each iteration prompts 'Your guess: ', reads with std::cin, and increments attempts before comparing. Counting the attempt before the comparison means the winning guess is counted too — players expect 'found in 5 attempts' to include the final guess.
while (guess != secret) {
std::cout << "Your guess: ";
if (!(std::cin >> guess)) return 0;
++attempts;
if (guess < secret) std::cout << "Too low!\n";
else if (guess > secret) std::cout << "Too high!\n";
}Step 4: Handle bad input inside the read
The if (!(std::cin >> guess)) return 0; guard means typing 'abc' ends the program politely instead of looping forever on a broken stream. It is one line, and it is the difference between a demo and a program you can hand to someone else.
Step 5: Announce the win with the attempt count
When the loop condition fails, the guess equals the secret, so print 'Correct in N attempts!'. Notice there is no separate win check inside the loop — the loop condition IS the win check. That is the pattern to remember: let the loop condition carry the game's end state.
Step 6: Add the play-again prompt from the checklist
Wrap the whole game in a do/while: after a win, ask 'Play again? (y/n)' and read a char. If it is 'y', draw a fresh secret with dist(rng) and reset attempts to 0 — forgetting to reset either one is the classic bug here, and the game feels broken immediately.
Step 7: Test with a strategy, then with chaos
Play using binary search: guess 50, then halve the range each time — you should always win within 7 attempts for 1–100, which proves the hints are truthful. Then test chaos: guess 0, 101, and letters, and confirm the program behaves sensibly every time.
Common mistakes
- Seeding with a fixed number like std::mt19937 rng(42), so the 'random' secret is the same every run.
- Using rand() % 100 + 1 and getting a biased, predictable sequence — use <random> distributions instead.
- Placing ++attempts after the win message logic or inside only one branch, so the count is wrong.
Compile and run
g++ -std=c++17 -o guessing_game guessing_game.cppSample run
$ ./guessing_game
I'm thinking of a number from 1 to 100.
Your guess:
> 50
Too high!
Your guess:
> 25
Too low!
Your guess:
> 37
Too low!
Your guess:
> 43
Too high!
Your guess:
> 40
Too low!
Your guess:
> 42
Correct in 6 attempts!Try it in your browser
This project has a browser demo in the classic site. The demo is a browser re-implementation of the same logic, not the compiled C++ program: The computer picked a number from 1 to 100. You get higher/lower hints — try to beat 7 guesses.