I was wondering if programming a hangman game would be possible. I took a little bit of time to make something, but it doesn't seem to be working very well. Any info is appriciated!

#include <iostream>
using namespace std;

int main()
{
	char solution[20];	//holds solution
	char blank[20];		//holds "*"'s for unsolved letters
	int counter = 0;	//general-use counter
	int right = 0;		//1 = right guess, 0 = wrong guess.
	char guess;

	cout<<"Enter phrase 20 chars or less."<<endl;
	cin.getline(solution, 20);
	int puzzLength = strlen(solution);		//finds lengtrh of puzzle, stores INT value to puzzlength
	



		//convert puzzle to full uppercase
	for (counter = 0; counter < puzzLength; counter++){
		solution[counter] = toupper(solution[counter]);
	}
	//done converting
		
	
	strcpy_s(blank, solution);				//copy solution to the 'blank' array
	
	for (counter = 0; counter < puzzLength; counter++) {		//converts characters to *'s to represent blanks

		if (isalnum(solution[counter])) blank[counter] = '*';
		else blank[counter] = solution[counter];
		
	}	//closes for loop

	while (solution != blank) { //play game until the 'blank' puzzle becomes the 'right' answer
		
		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);


		//cbeck guess!
		for (counter = 0; counter <= puzzLength; counter++) {
	
				if (guess == solution[counter]) { 
					blank[counter] = guess;		//fill in the puzzle with the letter
		
				}
	
		}		//close loop, done checking guess
	
	}	//game is over.

	cout<<"Winner!";
	cin.get();
	return 0;
}

but it doesn't seem to be working very well.

What isn't working very well? I tried the program and the only thing I found wrong is that it doesn't recognize it when you've solved the puzzle. That's because of this line: while (solution != blank) You're comparing two pointers to the first address of the arrays with each other. In other words: this will never be true.
Change it to: while (strcmp(solution, blank)) And it should work a lot better


A little advice: If you use C++ why not use std::string instead of char* ?

that was the problem I had, I was able to get the game started and running, but the game would never recognize when it was over.

Well if you change the line I mentioned, then that problem should be solved :)

3 Threads posting the same problem.. ? Doesn't anybody try to do their homework by themselves anymore ?? You could have all helped eachother with it and you would figured out the problem in no time. :P

commented: You could bash all their heads together, and still not get a spark of intelligence ;) +17

yea I noticed someone else copied my code like 20 minutes after I posted this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.