#include <iostream>
using namespace std;
#include <string>
#include <cctype>
void DrawGallows(int );
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(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<<"The current 'blank' puzzle is: "<<blank<<"."<<endl;
		cout<<"Enter a guess."<<endl;
		cin>>guess;
		guess = toupper(guess);


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

	}	//game is over.

	cout<<"Winner!";
	cin.get();
	return 0;
}
void DrawGallows(int State)
{
 if(State==6)
 {
  // The \\ will translate as '\' because it is a special char
  cout<<endl<<endl
   <<"   +----+     "<<endl
   <<"   |    |     "<<endl
   <<"   |    O     "<<endl
   <<"   |   /|\\   "<<endl
   <<"   |   / \\   "<<endl
   <<"   |Your Dead "<<endl
   <<"  ============"<<endl<<endl;
 }
 else if(State==5)
 {
  cout<<endl<<endl
   <<"   +----+  "<<endl
   <<"   |    |  "<<endl
   <<"   |    O  "<<endl
   <<"   |   /|\\ "<<endl
   <<"   |     \\ "<<endl
   <<"   |       "<<endl
   <<"  ============"<<endl<<endl;
 }
 else if(State==4)
 {
  cout<<endl<<endl
   <<"   +----+  "<<endl
   <<"   |    |  "<<endl
   <<"   |    O  "<<endl
   <<"   |   /|\\ "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"  ============="<<endl<<endl;
 }
 else if(State==3)
 {
  cout<<endl<<endl
   <<"   +----+  "<<endl
   <<"   |    |  "<<endl
   <<"   |    O  "<<endl
   <<"   |   /|  "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"  ============="<<endl<<endl;
 }
 else if(State==2)
 {
  cout<<endl<<endl
   <<"   +----+  "<<endl
   <<"   |    |  "<<endl
   <<"   |    O  "<<endl
   <<"   |    |  "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"  ============="<<endl<<endl;
 }
 else if(State==1)
 {
  cout<<endl<<endl
   <<"   +----+  "<<endl
   <<"   |    |  "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"   |       "<<endl
   <<"  ============="<<endl<<endl;
 }
}

THIS IS MY CODE.. AND I HAVE PROBLEMS WITH THAT :
1) WHEN I INPUT THE WRONG CHARACTER, IT RUNS UNTIL STATE =5 ,NOT AS IT SHOULD RUNS AT STATE= 1+1 = 2... HOW CAN i FIX THAT
THIS IS MY SAMPLE RUN :

Enter phrase 20 chars or less.
DSADS
Enter a guess.
D


+----+
| |
| O
| |
|
|
=============

+----+
| |
| O
| /|
|
|
=============

+----+
| |
| O
| /|\
|
|
=============

+----+
| |
| O
| /|\
| \
|
============

Enter a guess.

2) I JUST WANNA TO CONFIRM, IF I WANNA TO LINK THIS FILE TO MY MAIN FILE .CPP . I SHOULD NAME THIS FILE .H AND DEFINE IT IN THE MAIN FILE, AND USE THE FUNCTION?? THE FUNCTIONS DRAWGALLOW : DO I HAVE TO DECLARE AND SET UP THINGS LIKE THAT IN THE MAIN FILE AND IN THE .H FILE SEPERATELY. OR I CAN COMBINE THEM. BUT I JUST CAN YOU THE FUNCTION FROM THE .H FILE TO THE MAIN FILE. NOT FROM THE MAIN FILE TO THE .H FILE RITE?

This

while (solution != blank) { //play game until the 'blank' puzzle becomes the 'right' answer

is not going to work. You cannot directly compare the strings this way. Look up use of strcmp( ) function.

Your guess checking block seems to be flawed

//check guess!
int State =1; 
for (counter = 0; counter <= puzzLength; counter++) 
{
	if (guess == solution[counter]) 
	{ 
		blank[counter] = guess;		//fill in the puzzle with the letter
	}

	if (guess != solution[counter])
	{
		State++;
		DrawGallows(State);
	}
}		//close loop, done checking guess

You are adding to the degree of hanging for every guess that does not match every position of the word. If the guess matches any of the blank spaces in the solution, player should not be further harmed.

Try using a boolean value that gets set inside the loop to a good state if the user got a character correct. Initialize it before the loop to indicate bad guess. If you enter that if block, set the value to indicate a successful guess. After the checking loop, examine that value to determine if the state variable (degree of hanging) needs to be updated.

Also, the state variable should be set to 1 before entering the main guessing loop, not inside it.

This article has been dead for over six months. Start a new discussion instead.