Hello,
I am having trouble with this Hangman problem. In line 179, the wrong guesses are supposed to add up. I can't get the 'else' statement (line 176) to work. When i guess a wrong letter, it does not add up the wrongGuesses. What could be the problem?
Any help would be greatly appreciated!
Thank you

#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<string>
using namespace std;

int main()
{
    char phrase[80];
    char phraseDisplay[80];
    char letterDisplay[27];
    bool anotherPhrase=true;
    int  phraseLength;
	int  index;
    int  i,wrongGuesses,guessesToGo;
	bool stillAlive;
	bool puzzleIncomplete;
	char nextLetter;
	bool letterFound;
	char response;
	string displayPhrase;
	
	
while(anotherPhrase)
{
	cout<<"Enter Phrase: "<<endl;
	cin.getline(phrase,80);
    phraseLength=strlen(phrase);
	for(i=0;i<phraseLength;i++)
		phrase[i]=toupper(phrase[i]);
	for(i=0;i<phraseLength;i++)
	{
    if(isalpha(phrase[i]))
	   phraseDisplay[i]= '-';
    else
	   phraseDisplay[i]=phrase[i];
	}
	for(i=phraseLength;i<80;i++)
		phraseDisplay[i]= ' ';

	for(i=0;i<=25;i++)
	letterDisplay[i]=char(i+65);
	
	
	wrongGuesses=0;
	guessesToGo=6;
	stillAlive=true;
	puzzleIncomplete=true;
while(stillAlive&&puzzleIncomplete)
{
	
	cout<<"The Phrase"<<endl;
	cout<<endl;
	cout<<phraseDisplay<<endl;
	cout<<endl;
	cout<<letterDisplay<<endl;
	
	
	
	
	if(wrongGuesses==0)
	{
		
		cout<<endl<<endl
		<<"   +----+     "<<endl
		<<"   |    |     "<<endl
		<<"   |          "<<endl
		<<"   |          "<<endl
		<<"   |          "<<endl
		<<"   | Still Alive         "<<endl
		<<"  ============"<<endl<<endl;
		cout<<"Wrong Guesses: "<<wrongGuesses<<endl;
	}
	else if(wrongGuesses==1)
	{
		cout<<endl<<endl
		<<"   +----+  "<<endl
		<<"   |    |  "<<endl
		<<"   |    O  "<<endl
		<<"   |       "<<endl
		<<"   |       "<<endl
		<<"   | Still Alive      "<<endl
		<<"  ============"<<endl<<endl;
		cout<<"Wrong Guesses: "<<wrongGuesses<<endl;
	}
	else if(wrongGuesses==2)
	{
		cout<<endl<<endl
		<<"   +----+  "<<endl
		<<"   |    |  "<<endl
		<<"   |    O  "<<endl
		<<"   |    |  "<<endl
		<<"   |       "<<endl
		<<"   | Still Alive      "<<endl
		<<"  ============="<<endl<<endl;
		cout<<"Wrong Guesses: "<<wrongGuesses<<endl;
	}
	else if(wrongGuesses==3)
	{
		cout<<endl<<endl
		<<"   +----+  "<<endl
		<<"   |    |  "<<endl
		<<"   |    O  "<<endl
		<<"   |   /|  "<<endl
		<<"   |       "<<endl
		<<"   | Still Alive      "<<endl
		<<"  ============="<<endl<<endl;
		cout<<"Wrong Guesses: "<<wrongGuesses<<endl;
	}
	else if(wrongGuesses==4)
	{
		cout<<endl<<endl
		<<"   +----+  "<<endl
		<<"   |    |  "<<endl
		<<"   |    O  "<<endl
		<<"   |   /|\\  "<<endl
		<<"   |       "<<endl
		<<"   | Still Alive      "<<endl
		<<"  ============="<<endl<<endl;
		cout<<"Wrong Guesses: "<<wrongGuesses<<endl;
	}
	else if(wrongGuesses==5)
	{
		cout<<endl<<endl
		<<"   +----+  "<<endl
		<<"   |    |  "<<endl
		<<"   |    O   "<<endl
		<<"   |   /|\\   "<<endl
		<<"   |   /   "<<endl
		<<"   | Still Alive      "<<endl
		<<"  ============="<<endl<<endl;
		cout<<"Wrong Guesses: "<<wrongGuesses<<endl;
	}
	else if(wrongGuesses==6)
	{
		cout<<endl<<endl
		<<"   +----+   "<<endl
		<<"   |    |   "<<endl
		<<"   |    O   "<<endl
		<<"   |   /|\\  "<<endl
		<<"   |   / \\  "<<endl
		<<"   |You're Hung "<<endl
		<<"  ============="<<endl<<endl;
		cout<<"Wrong Guesses: "<<wrongGuesses<<endl;
	}
	

cout<<"Pick next letter: ";
cin>>nextLetter;
nextLetter=toupper(nextLetter);
index=(int)nextLetter-65;

if(isalpha(nextLetter))
{
	if(letterDisplay[index]!= ' ')
  {
		letterFound=false;
		i=0;
     while(i<phraseLength&&!letterFound)
     {
	   if(nextLetter==phrase[i]);
	     letterFound=true;
		 i++;
	 }
    if(letterFound)
    {
	   for(i=0;i<phraseLength;i++)
	   {
		if(phrase[i]==nextLetter)
			phraseDisplay[i]=nextLetter;
	   }
	    letterDisplay[index]= ' ';
	}
    else
    {
	letterDisplay[index]= ' ';
	wrongGuesses++;
	guessesToGo=6-wrongGuesses;
    }
  }
} 

puzzleIncomplete=false;
for(i=0;i<phraseLength;i++)
 {
  if(isalpha(phrase[i]) && phrase[i]!=displayPhrase[i])
	puzzleIncomplete=true;
 }
if(guessesToGo==0)
	stillAlive=false;
} 
	cout<<phrase<<endl;
	cout<<endl;
cout<<"Another Phrase? y/n: ";
	cin>>response;
}
	return 0;
}

Recommended Answers

All 3 Replies

if(nextLetter==phrase[i]);

There is a ; after the if, so letterFound will _always_ be set to true.

Edit:
Also, I think it would be cleaner to use for loops instead of while... the use of while is confusing in this case, and it is very easy to forget the i=0 before the while loop...
I'd say this is better:

for( i=0; i < phraseLength && !letterFound; ++i ) 
{

}

There are several mistakes in the above code. The main one is that you wrote the code in one long block without debugging each section. It is almost always easier to write the code in little bits, keeping a running program that does a little more each time.

Let us look at some of your common errors: Consider this code

while(i<phraseLength&&!letterFound)
   {
      if(nextLetter==phrase[i]);
      letterFound=true;
      i++;
   }

The first thing I did with your code was compile it with all the warnings on:
and I get several warnings and it tells me that I have an empty body in the if construction at 160.

The second thing I did, was reformat it with a code indenter. . That shows the problem. The if statement has an extra semi-colon so letterFound is ALWAYS true.


I am sure you can see your mistake now.


EDIT: theLamb beat me too it, sorry for the repeat, . But please compile with warnings and use an automatic code indenter. It really really helps show you were the errors are, just because things "look wrong".

commented: Put some good effort into this +3

Thanks for the suggestions guys. I can't believe i missed the semicolon. Again thanks so much!

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.