Hello, I really could use some help with this hangman program. I have searched all over the internet to find an answer that i can understand. I'm a beginner at C++ programming.

This program prompts the user to enter a phrase. then you guess letters. if a letter is wrong it will start building the noose. If you're right, the letters fill in where the '-' are located.


I have two problems at the moment. On line 54 letterDisplay is supposed to display the alphabet and take away the letter that is chosen. When i run the program, it does not display the alphabet at all. What could be wrong? The second problem starts on line 97. When i pick a letter, it just loops on if(wrongGuesses==0)<<<<[line 59]. What could be doing that?

Any help would be greatly appreciated. I am sorry the code is so long.

Thanks.

#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  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=1;i<=26;i++)
	   letterDisplay[i]=char(i+64);// letterDisplay is formed
	wrongGuesses=0;
	guessesToGo=6;
	stillAlive=true;
	puzzleIncomplete=true;
while(stillAlive&&puzzleIncomplete)
{
	
	cout<<"The Phrase"<<endl;
	cout<<phraseDisplay<<endl;
	
	cout<<letterDisplay<<endl;// This is supposed to display all letters in the alphabet.  It doesn't.
	
	
	
	
	if(wrongGuesses==0)
	{
		
		cout<<endl<<endl;
		//display
	}
	else if(wrongGuesses==1)
	{
		cout<<endl<<endl;
		//display
	}
	else if(wrongGuesses==2)
	{
		cout<<endl<<endl;
		//display
	}
	else if(wrongGuesses==3)
	{
		cout<<endl<<endl;
		//display
	}
	else if(wrongGuesses==4)
	{
		cout<<endl<<endl;
		//display
	}
	else if(wrongGuesses==5)
	{
		cout<<endl<<endl;
		//display
	}
	else if(wrongGuesses==6)
	{
		cout<<endl<<endl;
		//display
	}
	

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

if(isalpha(nextLetter))
{
	if(letterDisplay[i]!= ' ')
	{
		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[i]= ' ';
}
else
{
	letterDisplay[i]= ' ';
	wrongGuesses++;// wrongGuesses is formed
	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<<"Another Phrase? y/n: ";
	cin>>response;
}
	return 0;
}

I would suspect the letterDisplay cstring doesn't have anything in it. I think I can fix your problem without even having to double check your 'int to char' offset. Try this:

for(int i=0; i<26; i++)   
{
     letterDisplay[i] = 'A' + i;
}

letterDisplay[26] = '\0';

Change line 42, 43 to:

for(i=0;i<=25;i++)

letterDisplay[i]=char(i+65);// letterDisplay is formed

That got the letters to show. Thanks!

now whenIi choose a letter it only takes out the letter 'D'...
my guess is there is something wrong in line 100.
Any ideas?

That got the letters to show. Thanks!

now whenIi choose a letter it only takes out the letter 'D'...
my guess is there is something wrong in line 100.
Any ideas?

i got this to work. (well credit goes to you guys)

thanks so much.
Now my only problem is getting it to count up my wrong guesses.
Any suggestions there?

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.