
In this article, we will build a simple Number Guessing Game in C++. It’s a game in which the player has to guess a secret number in a range that is generated by the computer and upon a wrong guess by the player, the game will give hints to the player to guess the correct number. This will be a console game so even if you have little knowledge of C++, you will be able to follow up and build this game easily.
Features
- The player can choose the number of rounds he wants to play the game
- After finishing each round the current score is updated and once all the rounds are completed the high score is updated and shown on the screen.
- If the player guesses the correct number on the first try, he gets 10 points, If guessed on the second try then 5 points, if guessed on the third try then 3 points, and if guessed after the third try then only 1 point.
Code for Number Guessing Game in C++
We will use Code::Blocks IDE to develop this application. Open the Code::Blocks IDE and create a new project with the name guessthenumber, now in the main.cpp file paste the following code and run it.
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
system("color CF"); //This gives the terminal a color and the text on the screen
system("title GuessTheNumber @copyassignment"); //This sets the text on the terminal
int user_input,secret_num,c=0,wrong_attempts=0;
int curr_score=0, max_score=0;
int number_of_rounds;
int choice;
cout << "\t\t\t<==================================================================>" << endl;
cout << "\t\t\t| Welcome to the number guessing Game |" << endl;
cout << "\t\t\t<==================================================================>" << endl;
cout << endl << endl << endl;
cout << "\t\t\tchoose from below options" << endl;
cout << "\t\t\t [1] Know Game Rules" << endl;
cout << "\t\t\t [2] Play the game" << endl;
cin >> choice;
if(choice == 1){
system("cls");
cout << "\t\t\t<==================================================================>" << endl;
cout << "\t\t\t| Welcome to the number guessing Game |" << endl;
cout << "\t\t\t<==================================================================>" << endl << endl << endl;
cout << "\tChoose the number of rounds you want to play from 1-10" << endl;
cout << "\tIn each round, you have to guess the secret number correctly" << endl;
cout << "\tIf you guess the secret number in first attempt, you get 10 points" << endl;
cout << "\tIf you guess the secret number in second attempt, you get 5 points" << endl;
cout << "\tIf you guess the secret number in third attempt, you get 3 points" << endl;
cout << "\tIf you guess the secret number in after third attempt, you get only 1 point" << endl;
}
cout << endl << endl;
char ch;
do
{
cout << "\t Please enter number of rounds you want to play this game (1-5)" << endl;
cin >> number_of_rounds;
curr_score = 0;
wrong_attempts = 0;
// this loop run for the number of rounds the user wants to play
for(int i=0; i<number_of_rounds; i++){
secret_num=rand()%10+1; // generate a random secret number in the range 1-10
// until the user doesn't find the right answer keep on asking and checking
while(secret_num != user_input)
{
cout<<"\t Enter your guess between 1-10 "<<endl;
cin >> user_input;
cout<<endl;
if(user_input < secret_num){
cout << "\t Your guess is low, guess higher!!!" << endl;
wrong_attempts++;
}
else if(user_input > secret_num)
{
cout<<"\t Your guess is high, guess lower!!!"<<endl;
wrong_attempts++;
}
}
//when the user has guessed it right, check how quickly the number was found and give points accordingly
cout<<"\t Congratulations your guess is correct"<<endl << endl;
cout<<"\t You guessed wrong numbers " << wrong_attempts << " times" << endl << endl; //show how many times the number was guessed wrong
//if there is next round still remaining then show message
if(i!=number_of_rounds-1){
cout << "\t Let's start next round" << endl;
}
cout << endl << endl;
if(wrong_attempts == 0){
curr_score = curr_score + 10;
}
else if(wrong_attempts == 1){
curr_score = curr_score + 5;
}
else if(wrong_attempts == 2){
curr_score = curr_score + 3;
}
else{
curr_score = curr_score + 1;
}
}
//set the high score if it's greater than previous score
if(curr_score > max_score){
max_score = curr_score;
}
system("cls");
cout << "\t\t\t You have completed all the rounds" << endl;
cout << "\t\t\t Your current score is: " << curr_score << endl;
cout << "\t\t\t Highest score: " << max_score << endl << endl << endl;
cout<<"Do you want to play again y/n";
cin >> ch;
cout<<endl;
if(ch !='n' && ch !='y'){
cout << "\tWrong Input, Please enter y/n" << endl;
}
system("cls");
cout << "\t\t\t<==================================================================>" << endl;
cout << "\t\t\t| Welcome to the number guessing Game |" << endl;
cout << "\t\t\t<==================================================================>" << endl;
}while(ch!='n');
}Output of Number Guessing Game in C++
Image Output:

Video Output:
Conclusion
I hope this article on Number Guessing Games in C++ could help you learn something valuable. Building these types of small games can help you get good at logic building and learn a language faster. You should try to tweak the code here and there and see how the whole program is working.
Thank you for visiting our website.
Also Read:
- You Can Now Run AI Fully Offline on Your Phone — Google’s Gemma 4 Just Changed Everything
- I Built a 24×7 AI Blogging System for WordPress Using Python (Free) — Full Code Inside
- This Reddit User “Hacked” AI With Simple Tricks… And The Results Are Insane
- One “rm -rf” Command Almost Wiped Out $100 Million Worth of Toy Story 2
- How to Make Money with ChatGPT in 2026: A Real Guide That Still Works
- PicoClaw vs OpenClaw: The Tiny AI That Could Replace Powerful AI Agents
- Oracle Layoffs 2026: People Woke Up to an Email… and Lost Their Jobs Instantly
- X’s New Video Update Is Breaking a Basic Feature — And Users Are Not Happy
- The Most Shocking Military Tech Yet: Robot Soldiers That Could Change Warfare Forever
- Sora Shutdown: The Reality Check That Shook AI Video — And What Comes Next
- Aya Expanse supports multiple languages for diverse global applications
- Alibaba releases Page Agent on GitHub for public access
- Google Sheets Gemini reaches new levels of performance and accuracy
- Artificial intelligence boosts cardiac care in rural Australian communities
- NVIDIA GTC 2026 Offers Insights into Future Artificial Intelligence Developments
- Google DeepMind Updates Satellite Embedding Dataset with 2025 Data
- Enhancing hierarchical instruction in advanced large language models
- Meta supports community development near its data centers through grants
- Exploring the world of underwater robotics through coding techniques
- ABB Robotics partners with NVIDIA for large scale physical AI solutions
- Why All AI Models Are Slowly Becoming the Same Model
- Aam Aadmi vs Corrupt System: How ChatGPT Helped One Guy Expose Govt Fraud, The Story: “Ravi and The Missing Light Pole”
- ChatGPT Asked a person to commit suicide to solve the problem
- Viral Moment: China’s AgiBot X2 Makes History With World’s First Webster Backflip
- Terminator Rising: Albania Hands Power to AI, Echoing a Nightmare of Human Extinction
- What Is Albania’s World-First AI-Generated Minister and How Does It Work?
- Does ChatGPT believe in God? ChatGPT’s Personal Opinion
- ChatGPT vs Human: The Breath-Holding Chat That Ends in “System Failure”
- What Is Vibe Coding? The Future of No-Code Programming and Its Impact on Software Developers
- Struggling to Generate Ghibli-Style AI Images? Here’s the Real Working Tool That Others Won’t Tell You About!



