A Number Guessing Game
Implement a small interactive number guessing game with random numbers.
A Number Guessing Game is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Game Plan
The program picks a secret number 1..100. The user guesses; the program says "too high" or "too low." Win after a fixed number of attempts or when guessed correctly.
Required Headers
For random numbers and I/O.
#include <iostream>
#include <random>
#include <string>Generating the Secret
Use the modern <random> facilities. Seed once.
std::random_device rd;
std::mt19937 engine(rd());
std::uniform_int_distribution<int> dist(1, 100);
int secret = dist(engine);Main Loop
Prompt, read, compare, repeat until correct.
int guess;
int attempts = 0;
while (true) {
++attempts;
std::cout << "Guess (1-100): ";
if (!(std::cin >> guess)) break;
if (guess == secret) {
std::cout << "Got it in " << attempts << " tries\n";
break;
}
std::cout << (guess < secret ? "too low" : "too high") << "\n";
}Limiting Attempts
Add a maximum number of tries to make the game harder.
const int MAX = 10;
if (attempts >= MAX) {
std::cout << "Out of tries! It was " << secret << "\n";
break;
}Validating Input
If the user types non-numeric input, cin goes into fail state. Recover gracefully.
if (!(std::cin >> guess)) {
std::cin.clear();
std::cin.ignore(10000, '\n');
std::cout << "Please enter a number\n";
continue;
}Out-of-Range Guesses
Reject numbers outside 1..100 before counting an attempt.
if (guess < 1 || guess > 100) {
std::cout << "Out of range\n";
--attempts; // not a real attempt
continue;
}Replay Loop
After a game ends, ask if the user wants to play again.
std::cout << "Play again? (y/n): ";
char reply;
std::cin >> reply;
if (reply != 'y') break;Tracking Statistics
Keep totals across games — best score, average attempts, total wins.
int wins = 0;
int bestAttempts = INT_MAX;
// update inside the win branchCustomizing the Range
Let the user pick easy (1..10), medium (1..100), or hard (1..1000) modes. Adjust dist accordingly.
Hint System
Optional: after several wrong guesses, narrow the range. "It is at least 40."
Splitting into Functions
Refactor into generateSecret(), readGuess(), playRound(), and main(). Each function does one thing.
Quick Check
Which sequence of header includes is needed for a modern C++ random number?
Recap
A number guessing game ties together random number generation, input validation, loops, and game state. Refactor into small functions and add replay, stats, and difficulty for polish.
Frequently asked questions
Is the “A Number Guessing Game” lesson free?
Yes — the full text of “A Number Guessing Game” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “A Number Guessing Game”?
Implement a small interactive number guessing game with random numbers. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “A Number Guessing Game” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Building a Simple Calculator CLI
- Reading and Writing CSV Files
- A Number Guessing Game
- A Word Frequency Counter