0% found this document useful (0 votes)
85 views6 pages

Hangman

This C++ program implements a basic hangman game. It begins by getting a secret word from the user and initializing variables to track the game state, including the number of incorrect guesses remaining. It then repeatedly prompts the user for a letter guess, checks if it is in the secret word, updates the display with correct guesses, and checks for winning/losing conditions until the user either guesses the full word or runs out of guesses. The display is updated each round to show the current guessed letters and remaining blanks until the game concludes.

Uploaded by

Reema Malve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views6 pages

Hangman

This C++ program implements a basic hangman game. It begins by getting a secret word from the user and initializing variables to track the game state, including the number of incorrect guesses remaining. It then repeatedly prompts the user for a letter guess, checks if it is in the secret word, updates the display with correct guesses, and checks for winning/losing conditions until the user either guesses the full word or runs out of guesses. The display is updated each round to show the current guessed letters and remaining blanks until the game concludes.

Uploaded by

Reema Malve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

#include<iostream.

h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

void showHangman(int);

int main(void)

char hangmanWord[100], tempWord[100]; /**hangmanWord[] array for the original word and
tempWord[] array to get the alphabet from user and compare it with original word**/

char hangmanOutput[100]; /**This array will show the remaining blanks and correct
inputs**/

int wrongTry = 6 , matchFound = 0; /**player will get 5 chance, so we use wrongTry as


chance counter**/

/**matchFound to search the alphabet, if the


alphabet from user does not exist

in the original word it will remain 0, upon finding


the word, matchFound will

be set as 1**/

int counter = 0 , position = 0, winner, length , i;

char alphabetFromUser;

clrscr(); /**for clearing the screen**/

cout<<"\n\n Enter any word in small case and hit >>ENTER<<";

cout<<"\n\n\t Enter HERE ==> ";

cin>>hangmanWord; /**get the string from opponent**/

cout<<"\n\n Now give the COMPUTER to your friend and see if he/she can CRACK it!!!";

cout<<"\n\n\tHIT >>ENTER<<";

getch();

length = strlen(hangmanWord); /**get the length of the word**/


clrscr();

cout<<"\n\n !!!!!!!!!!!!!!!!!!!Welcome to the HANGMAN GAME!!!!!!!!!!!!!!!!!\n\n\n"; /**Brief


description of the game**/

cout<<"\n\n You will get 5 chances to guess the right word";

cout<<"\n\n So help the Man and get...set...GO..!!";

cout<<"\n\n\tHIT >>ENTER<< ";

getch();

clrscr();

cout<<endl<<"\n\n The word has %d alphabets \n\n"<<length; /**tell the user how many
alphabets the word has**/

for( i = 0; i < length ; i++)

hangmanOutput[i] = '_';

hangmanOutput[length] = '\0';

for(i = 0 ; i < length ; i++)

cout<<" ";

cout<<endl<<"%c"<<hangmanOutput[i]; /**Show the Word With n(length of the original


word) number of underscores (_)**/

while(wrongTry != 0) /**while loop for exiting the program when no try left**/

matchFound = 0;

cout<<"\n\n enter any alphabet from a to z and please use small case!!";

cout<<"\n\n\t Enter HERE ==> ";


//fflush(stdin);

cin>>alphabetFromUser; /**get alphabet from user**/

if(alphabetFromUser < 'a' || alphabetFromUser > 'z') /**In case player gives input other than
'a' to 'z' the console will ask again**/

clrscr;

cout<<"\n\n\t Wrong input TRY AGAIN ";

matchFound = 2;

// fflush(stdin);

if(matchFound != 2)

for(counter=0;counter<length;counter++) /**for loop to check whether player input


alphabet exists or not in the word**/

if(alphabetFromUser==hangmanWord[counter])

matchFound = 1;

}//end of if()

if(matchFound == 0) /**in case of wrong guess**/

cout<<endl<<"\n\t :( You have %d tries left "<<--wrongTry;}//end of if()

else

for(counter = 0; counter < length; counter++)

{
matchFound = 0;

if(alphabetFromUser == hangmanWord[counter])

position = counter ;

matchFound = 1;

}//end of if

if(matchFound == 1)

for(i = 0 ; i < length ; i++)

if( i == position)

hangmanOutput[i] = alphabetFromUser; /**Put the alphabet at


right position**/

else if( hangmanOutput[i] >= 'a' && hangmanOutput[i] <= 'z' )

continue;

else

hangmanOutput[i] = '_'; /** Put a blank at not


guessed alphabet position **/

tempWord[position] = alphabetFromUser; }

}//end of for
tempWord[length] = '\0'; /**put the NULL character at the end of the
temp string**/

winner = strcmp(tempWord,hangmanWord); /**upon True comparison it will


return 0**/

if(winner == 0) /**if the player guessed the whole word right then
he/she is the WINNER**/

cout<<"\n\n\t \t YAHOO!!!!! You are the WINNER !!!!!";

cout<<endl<<"\n\n\t The Word was %s "<<hangmanWord;

cout<<"\n\n\n\n\t\tEASY HUH???\n\n";

getch();

return 0;

cout<<"\n\n\t";

for(i = 0 ; i < length ; i++)

cout<<" ";

cout<<endl<<"%c"<<hangmanOutput[i]; /**Show the original Word With blanks


and right Input alphabet**/

if(wrongTry <= 0) /**if the player can not guess the whole word in 5 chaces**/

cout<<endl<<"\n\n\t The Word was %s "<<hangmanWord;

cout<<"\n\n\t The man is dead!!!!!";

cout<<"\n\n\t Better luck next!!!"; }


getch();

return 0;

You might also like