So I have to work on a hangman game, but im having some problems with it. It keeps showing the word you enter and does not stop the game when you are out of tries (when you hang the man lol) I was wondering if i can get some help in fixing some of these problems. Also, any advice as to how to print out the hangman picture as the game is going would be fantastic. here is my code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
using namespace std;
int main()
{
char solution[100];
char blank[100];
int counter = 0;
int right = 0;
char guess;
cout<<"Enter phrase 100 chars or less."<<endl;
cin.getline(solution, 100);
int puzzLength = strlen(solution);
for (counter = 0; counter < puzzLength; counter++){
solution[counter] = toupper(solution[counter]);
}
strcmp(blank, solution);
for (counter = 0; counter < puzzLength; counter++) {
if (isalnum(solution[counter])) blank[counter] = '*';
else blank[counter] = solution[counter];
}
while (strcmp( blank, solution ) != 0) {
cout<<endl<<"Solution phrase is: "<<solution<<"."<<endl;
cout<<"The current 'blank' puzzle is: "<<blank<<"."<<endl;
cout<<"Enter a guess."<<endl;
cin>>guess;
guess = toupper(guess);
for (counter = 0; counter <= puzzLength; counter++) {
if (guess == solution[counter]) {
blank[counter] = guess;
}
}
}
cout<<"Winner!";
cin.get();
return 0;
}
|
Here were also some guidelines...
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.
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
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
Thanks