0% found this document useful (0 votes)
3K views

Hangman C++ Code

The document defines a Hangman program in C++ that allows a player to guess a random country within 5 attempts. The program initializes the word to be guessed as stars and replaces stars with correct guesses. It includes functions for initializing the unknown word and checking letter matches. The sample input and output shows gameplay where the player correctly guesses the word "oman" within 5 attempts.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Hangman C++ Code

The document defines a Hangman program in C++ that allows a player to guess a random country within 5 attempts. The program initializes the word to be guessed as stars and replaces stars with correct guesses. It includes functions for initializing the unknown word and checking letter matches. The sample input and output shows gameplay where the player correctly guesses the word "oman" within 5 attempts.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

HANGMAN PROGRAM DEFINITION: To write a program code for Hangman (A word guessing game) in C++.

PROGRAM ANALYSIS: The player is given maximum of five chances to guess the name of a country. The player is given one among 10 countries to guess. random() is used for this purpose. The letters to be guessed are represented by *.The player now enters the letter .The game continues till he guesses the complete word or he loses all his chances. The player is given a maximum of 5 chances to guess incorrectly. For this iteration statement has been used.2 functions are created to initialise the word and replace the * with the correct guesses.

PROGAM CODE: #include <iostream.h> #include <stdlib.h> #include <string.h> #include <conio.h> const int MAXLENGTH=80; const int MAX_TRIES=5; int letterFill (char, char[], char[]); void initUnknown (char[], char[]); int main () { char unknown [MAXLENGTH]; char letter; int num_of_wrong_guesses=0; char word[MAXLENGTH]; char words[][MAXLENGTH] = { "india", "pakistan", "nepal", "malaysia", "philippines", "australia", "iran", "ethiopia", "oman", "indonesia" }; randomize(); int n=random(10); strcpy(word,words[n]);

initUnknown(word, unknown); cout << "\n\nWelcome to hangman...Guess a country Name"; cout << "\n\nEach letter is represented by a star."; cout << "\n\nYou have to type only one letter in one try"; cout << "\n\nYou have " << MAX_TRIES << " tries to try and guess the word."; while (num_of_wrong_guesses < MAX_TRIES) { cout << "\n\n" << unknown; cout << "\n\nGuess a letter: "; cin >> letter;

if (letterFill(letter, word, unknown)==0) { cout << endl << "That letter isn't in there!" << endl; num_of_wrong_guesses++; } else { cout << endl << "You found a letter! " << endl; } cout << "You have " << MAX_TRIES - num_of_wrong_guesses; cout << " guesses left." << endl; if (strcmp(word, unknown) == 0) { cout << word << endl; cout << "Yeah! You got it!"; break; } } if(num_of_wrong_guesses == MAX_TRIES) { cout << "\nSorry, you lose...you've been hanged." << endl; cout << "The word was : " << word << endl; } getch(); return 0; } int letterFill (char guess, char secretword[], char guessword[]) { int i; int matches=0; for (i = 0; secretword[i]!='\0'; i++) { if (guess == guessword[i]) return 0; if (guess == secretword[i]) { guessword[i] = guess; matches++; } } return matches; } void initUnknown (char word[], char unknown[]) {

int i; int length = strlen(word); for (i = 0; i < length; i++) unknown[i]='*'; unknown[i]='\0'; }

SAMPLE INPUT AND OUTPUT: Welcome to hangman...Guess a country Name Each letter is represented by a star. You have to type only one letter in one try You have 5 tries to try and guess the word. **** Guess a letter: o You found a letter ! You have 5 guesses left. o*** Guess a letter:m You found a letter ! You have 5 guesses left. om** Guess a letter:d That letter isnt in there! You have 4 guesses left. om** Guess a letter:a You found a letter ! You have 4 guesses left. oma* Guess a letter:n You found a letter ! You have 4 guesses left. oman Yeah! You got it.

You might also like