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;
}