Write a C++ program that will create the hangman game using only strings ad no a
ID: 3759025 • Letter: W
Question
Write a C++ program that will create the hangman game using only strings ad no arrays. The program allows the user to enter a secret message and print the dashes for the message. The game will ask users to guess letters to determine the message and after each guess the program prints out -
number of letters found for the guess, letters that have been guessed, how many incorrect guesses are allowed , message with correctly guessed slots and blank slots and if the user guessed all the letters in the message then a correctly guessed slots and blank slots
Enter a message: Hi
The message is _ _
Guess a letter: t
There is no t. Y
ou have guessed: t You have 1 incorrect out of 5.
The message is _ _ Guess a letter: h There is 1 h.
You have guessed: t h You have 1 incorrect out of 5.
The message is h_ Guess a letter: i
There is 1 i. You have guessed: t h i
You have 1 incorrect out of 5.
The message is hi You won!!!
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[20],g;
int i,j,n=0,d;
cout<<"Enter a message:";
cin>>s;
d=strlen(s);
while(n!=d)
{
cout<<" The message is ";
for(i=n;i<strlen(s);i++)
{
cout<<"_";
}
cout<<" Guess a letter: ";
cin>>g;
for(i=n;i<strlen(s);i++)
{
if(g==s[i])
{
cout<<" You have 1 "<<g;
n=n+1;
break;
}
}
if(i==strlen(s))
{
cout<<" There is no "<<g;
}
}
cout<<"The message is "<<s<<"You Won!!!";
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.