Hangman

These are the instructions. How do I include all of the requirements below. *My program is listed beneath the instructions.
________________________________________________________________________________
You'll ask the user to enter a secret word or phrase, a 100 char array
Any letter that gets entered will be replaced with a "*"
Spaces will be represented with a "_"
Any other character will be left alone.

Please enter a secret word or phrase:
>hello world

The secret word is: *****_*****

Next, the user will be asked to guess a letter. If the letter is found in the secret word, that letter will be displayed:

Enter your guess:
>l

The letter was found!
The secret word is: **ll*_***l*
Guessed Letters: l

For every correct guess, the letter in the proper place will be revealed.
For every wrong guess, a body part of the hangman will be printed out:

head
head - body
head - body - left arm
head - body - left arm - right arm
head - body - left arm - right arm - left leg
head - body - left arm - right arm - left leg - right leg - HANGMAN!

Requirements:
You need to keep track of previously guessed letters, so the user will not guess the same letter twice.
You will have a function that takes a char array and returns a char array of *'s of the same length as the original char array
You will have a function that takes a char and a char array, and checks to see if that char is in the char array
You will probably also want a function that will display that reveals the hidden letter with a correct guess
You may also want a function that keeps track of guessed letters
and also a function that determines the state of the hangman, if the user fails to guess the word before "HANGMAN!", display the word.
________________________________________________________________________________
More instructions:
1. To make it easier, first how do I make the program hide the word entered by player1 and replace it with * or _ for letters and spaces.

2. When I run the program, it says that guess is not initialized, how do I fix that.

3. How do I fix this program so that it continually lets the user guess until they use all 6 of their try's.

________________________________________________________________________________

****My Program*****


#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;

int main()
{
int letter;
char guess;
char solution[100];
char newword[100];
char newletter;
int n=0;

cout << "***** Welcome to Hangman *****" << endl << endl;
cout << "Player1 type in a word: " << endl;
cin >> newword[100];
cin.getline(newword,100);


for(int n=0; n<100; n++)
{
if (newword[n] != ' ')
newword[n] = '*';
else
newword[n] ='_';
}

newword[20] = '\0';
cout << newword << endl << endl;


cout << "Player2 you have 6 try's to guess the secret word" << endl;
retry:
cout << "Player2 guess a letter: " << endl << endl;
cin >> letter >> guess;

while (newword[n] != '\0' && guess != 1)
{
if (newword[n] == letter)
{
guess = 1;
}
n++;
cout << "Correct!" << newword << endl;
}


if (letter != 0)
{
newword[n] = letter;
newword[n+1] = '\0';
}
else
{
cout << "Letter already entered, choose another letter" << endl;
}
cout << guess << letter << endl;
goto retry;


for(int n=0; n<100; n++)

{

if (newword[n] = '\0' && letter != 0)
{
cout << "Incorrect, Please Try Again." << endl;
goto retry;
}
}

for(int n=0; n<100; n++)
{
if (letter==1)
{
cout<<endl<<"Head";
}
if (letter==2)
{
cout<<endl<<"Head-Body";
}
if (letter==3)
{
cout<<endl<<"Head-Body-LeftArm";
}
if (letter==4)
{
cout<<endl<<"Head-Body-LeftArm-RightArm";
}
if (letter==5)
{
cout<<endl<<"Head-Body-LeftArm-RightArm-LeftLeg";
}
if (letter==6)
{
cout<<endl<<"Head-Body-LeftArm-RightArm-LeftLeg-RightLeg";
cout<<endl<<"You Lost! HANGMAN!!"<<endl<<endl;
}
}

cout << newword;
cout << "YOU WIN!!" << endl;

system("pause");
return 0;
}

Last edited on
Topic archived. No new replies allowed.