Skip to content
C++ Better Explained
Go back
C++ Number Guessing Game: A Complete Beginner Project Step by Step
Edit page

C++ Number Guessing Game: A Complete Beginner Project Step by Step

The number guessing game is the ideal first C++ project: the computer picks a secret number, you guess, and it tells you “higher” or “lower” until you find it. In about 40 lines you’ll combine random numbers, loops, conditionals, and user input — every fundamental skill in one program.


Step 1: generate the secret number

We’ll use the modern <random> library rather than the old rand() — it gives evenly distributed numbers and no surprising patterns:

#include <iostream>
#include <random>

int main() {
    std::random_device rd;                            // hardware seed
    std::mt19937 engine(rd());                        // random engine
    std::uniform_int_distribution<int> dist(1, 100);  // range 1-100

    int secret = dist(engine);
    std::cout << "Secret (for testing): " << secret << '\n';
    return 0;
}

mt19937 is the generator; uniform_int_distribution shapes its output into a fair 1–100 range. This avoids the modulo bias that rand() % 100 suffers from. Full details in our random numbers guide.


Step 2: the guess loop

Now add the game loop — read a guess, compare, give a hint, repeat:

#include <iostream>
#include <random>

int main() {
    std::random_device rd;
    std::mt19937 engine(rd());
    std::uniform_int_distribution<int> dist(1, 100);

    int secret = dist(engine);
    int guess = 0;
    int attempts = 0;

    std::cout << "I'm thinking of a number between 1 and 100.\n";

    while (guess != secret) {
        std::cout << "Your guess: ";
        std::cin >> guess;
        attempts++;

        if (guess < secret) {
            std::cout << "Too low!\n";
        } else if (guess > secret) {
            std::cout << "Too high!\n";
        } else {
            std::cout << "Correct! You got it in "
                      << attempts << " attempts.\n";
        }
    }

    return 0;
}

The while (guess != secret) loop is the heart of the game: it keeps running exactly as long as the player hasn’t won. The if/else-if chain handles the three possible outcomes of every guess. If these constructs are new, our loops tutorial and conditionals tutorial cover them from scratch.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Step 3: handle bad input

Type “abc” instead of a number and the current version loops forever — cin fails and stops reading. Defensive input handling fixes it:

#include <limits>

// inside the while loop, replace the cin line with:
if (!(std::cin >> guess)) {
    std::cin.clear();  // reset the error state
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "Please enter a number.\n";
    continue;          // skip to the next loop iteration
}

cin.clear() resets the stream’s error flag, and cin.ignore(...) throws away the junk input so it isn’t re-read forever. Every interactive program needs this pattern — it’s explained in depth in our cin user input guide.


Step 4: add replay and a guess limit

Two finishing touches make it feel like a real game. Wrap everything in a do-while for replay, and end the game after 7 guesses:

char again = 'n';

do {
    int secret = dist(engine);
    int guess = 0;
    int attempts = 0;
    const int MAX_ATTEMPTS = 7;

    while (guess != secret && attempts < MAX_ATTEMPTS) {
        // ... guess loop from Step 2/3 ...
    }

    if (guess != secret) {
        std::cout << "Out of guesses! It was " << secret << '\n';
    }

    std::cout << "Play again? (y/n): ";
    std::cin >> again;
} while (again == 'y' || again == 'Y');

Why 7? Each smart guess can halve the remaining range, and 2^7 = 128 > 100 — so 7 guesses always suffice if the player guesses the middle every time. You’ve just smuggled the idea of binary search into a game.


What you practiced

One small project, five fundamentals: random number generation, while and do-while loops, if/else decision chains, robust cin input handling, and using a named constant for the guess limit. When you’re ready for a bigger challenge, try extending it — difficulty levels that change the range, a high-score counter, or reversing the roles so the computer guesses your number using binary search.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
C++ const Keyword Explained: Variables, References, and Functions
Next Post
How to Reverse a String in C++: 4 Simple Methods Explained