This blog post is for the simple C++ code for Hangman Game. Discover how to build a simple hangman game in C++ with this step-by-step guide. Get the complete C++ code and understand how it works with our detailed explanation. In this post, we’ll explore how to create a basic version of this classic word-guessing game. Enhance your programming skills by learning key concepts like user interaction, control structures, and string manipulation in an engaging and fun way.
What is Hangman?
Hangman game is a popular guessing game where one player thinks of a word, and the other player tries to guess the word by suggesting letters. For each letter that is not in the word, the first player draws a part of a hangman’s gallows. The game continues until the word is guessed or the gallows is complete and the player who is guessing the word is “hanged.“

Game Overview
Hangman game in C++ is basically a word-guessing game where the player attempts to guess a secret word by suggesting letters within a certain number of guesses. The player is notified of their progress, and incorrect guesses reduce the number of remaining attempts. The game continues until the player either guesses the word correctly or runs out of guesses.
Key Components
Our hangman game implementation involves the following key components:
- User Interaction:
- The game begins by asking for the player’s name and welcoming them.
- The player is prompted to guess letters to uncover the secret word.
- Game Logic:
- A predefined secret word is used.
- The game tracks the player’s guesses and the number of remaining attempts.
- Correct guesses reveal letters in the word, while incorrect guesses decrease the number of remaining attempts.
- Control Structures:
- A
while
loop allows the game to continue until the player either wins or loses. - Conditional statements determine whether a guessed letter is in the word and update the game state accordingly.
- A
NOTE
Download the code by clicking on this button
or from Git Hub Repository
Breaking Down the C++ project of Hangman Game
In this section, we’ll dive deeper into the code and explain each part in more detail.
- Welcoming the User
The game starts by welcoming the user and asking for their name. This is done using the getline
function, which reads a line of input from the user and stores it in the name
variable.
string name;
cout << "What is your name? ";
getline(cin, name);
cout << "Hello, " << name << ", Time to play hangman!" << endl;
- Delay Between Turns
After welcoming the user, the game adds a brief delay of 1 second using the this_thread::sleep_for
function from the <thread>
library. This is done to create a more realistic experience and give the user a chance to read the welcome message.
- Setting the Secret Word
The game then sets a secret word, which in this case is “secret”. This word is the one that the user will try to guess.
- Initializing the Guesses
The game initializes an empty string guesses
to store the user’s guesses.
- Setting the Number of Turns
The game sets the number of turns the user has to guess the word. In this case, the user has 10 turns.
- The Main Game Loop
The game then enters a while
loop, which continues until the user either guesses the word correctly or runs out of turns.
- Displaying the Word
Inside the loop, the game displays the current state of the word. It does this by iterating over each character in the word and checking if the character is in the guesses
string. If it is, the game prints the character. If not, it prints a dash.
for (char& char_in_word : word) {
if (guesses.find(char_in_word) != string::npos) {
cout << char_in_word;
} else {
cout << "_";
failed++;
}
}
- Checking for Victory
After displaying the word, the game checks if the user has won by checking if the failed
counter is zero. If it is, the game prints a “You won” message and exits the loop.
- Getting the User’s Guess
The game then asks the user to guess a character and stores the input in the guess
variable.
- Updating the Guesses
The game updates the guesses
string by adding the user’s guess.
- Checking if the Guess is Correct
The game checks if the user’s guess is in the secret word. If it’s not, the game decrements the turn counter and displays a “Wrong” message.
- Checking for Defeat
If the turn counter reaches zero, the game prints a “You Lose” message and exits the loop.
Conclusion of C++ code for hangman game
You’ve now successfully implemented a basic Hangman game in C++. This project introduces fundamental C++ concepts and provides a solid starting point for expanding and enhancing the game. You can further improve the game by adding features such as:
- Graphics: Implementing visual representations of the hangman.
- Word Selection: Randomly selecting words from a predefined list.
- Multiplayer: Allowing multiple players to take turns guessing.
Source Code of hangman game in C++
Creating a hangman game in C++ is a fun and educational project that can help you practice and understand the fundamentals of C++ programming. Improve your C++ skills and create your own interactive games.
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
using namespace std;
int main() {
// Welcoming the user
string name;
cout << "What is your name? ";
getline(cin, name);
cout << "Hello, " << name << ", Time to play hangman!" << endl;
// Wait for 1 second
this_thread::sleep_for(chrono::seconds(1));
cout << "Start guessing..." << endl;
this_thread::sleep_for(chrono::milliseconds(500));
// Here we set the secret. You can select any word to play with.
string word = "secret";
// Creates a variable with an empty value
string guesses = "";
// Determine the number of turns
int turns = 10;
// Create a while loop
// Check if the turns are more than zero
while (turns > 0) {
// Make a counter that starts with zero
int failed = 0;
// For every character in secret_word
for (char& char_in_word : word) {
// See if the character is in the player's guess
if (guesses.find(char_in_word) != string::npos) {
// Print out the character
cout << char_in_word;
}
else {
// If not found, print a dash
cout << "_";
// And increase the failed counter with one
failed++;
}
}
cout << endl;
// If failed is equal to zero
if (failed == 0) {
// Print You Won
cout << "You won" << endl;
// Exit the script
break;
}
// Ask the user to guess a character
char guess;
cout << "Guess a character: ";
cin >> guess;
// Set the player's guess to guesses
guesses += guess;
// If the guess is not found in the secret word
if (word.find(guess) == string::npos) {
// Turns counter decreases by 1 (now 9)
turns--;
// Print wrong
cout << "Wrong" << endl;
// How many turns are left
cout << "You have " << turns << " more guesses" << endl;
// If the turns are equal to zero
if (turns == 0) {
// Print "You Lose"
cout << "You Lose" << endl;
}
}
}
return 0;
}