Game

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
I could be wrong about this, but to me, it looks like your while loop (starting at Line 29) isn't correct. You've got the loop outputting the solution if your comparison is != 0. I would think you only want to display the solution once strcmp() == 0.

It also looks like you've got your for loop (where I think you are trying to limit the number of tries) inside the strcmp while loop, which is part of why you can keep guessing. One thing you might try is changing your while statement to be strcmp() != 0 && counter <= puzzLength.

Someone with far greater experience then me can probably give better advice though.
When i put strcmp() !=0 && counter <=puzzLength, it does not work. Is there another way of doing it?
Okay, I didn't fully understand what your for loop was doing before. Now that I've looked a little closer, I get it.

You still need to remove Line #31 (from above) from the while loop, as that is why the solution is showing every time.

does not stop the game when you are out of tries (when you hang the man lol)

You don't have anything there that is keeping track of the tries. Here's how I changed your while loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int attempts = 0;
while (strcmp( blank, solution ) != 0 && attempts <=7) //used 7 as a default # of attempts
{ 
	//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);
	attempts++;

	for (counter = 0; counter <= puzzLength; counter++)
{
	if (guess == solution[counter])
{ 
	blank[counter] = guess;
	attempts--;    //don't count a correct guess as an attempt		
}
}	
}


Another thing I noticed is that you declare a Winner when they run out of guesses. You'll want to come up with a way to show Winner only if they win, and something else if they 'hang the man'.

I hope that helps you keep going in the right direction.
Thanks so much for the help!
Topic archived. No new replies allowed.