-
Hangman program Help!
I made this hangman program and everything works except I have been trying to put a variable named count which adds one everytime an incorrect letter is guessed and after six incorrect guesses the program quits, but i dont know where to put it so that it will work any help would be appreiciated here is my code:
Code:
#include<iostream.h>
#include<console.h>
#include<string.h>
int main()
{
int count=0;
int wrong;
string secret;
char answer;
string answer2;
string cover;
cout<<"Enter word: "<<endl; //User enters word to be guessed
cin>>secret;
cover=secret;
answer2=secret;
clrscr();// clears screen
for(int i=0; i<secret.length(); i++)//prints dashs to show length of word
{
cover[i]='-';
cout<<cover[i];
}
cout<<endl;
do{
cout<<"Enter your guess($ to guess the whole word and * to quit): "<<endl;
cin>>answer;
if(answer=='$') // Allows you to guess the whole word
{
cout<<"Enter your letter guess for the whole word: ";
cin>>answer2;
if(secret==answer2) // checks if word entered is right
{
cout<<"You are right!"<<endl;
answer='*';
}
else
{
cout<<"incorret, guess again"<<endl;
}
}
else
{
for(int j=0; j<secret.length(); j++) //changes dashs to correct letters guessed
{
if(secret[j]==answer)
{
cover[j]=answer;
wrong=true;
}
cout<<cover[j];
}
cout<<endl;
if(wrong!=true)
count++;
if(count==1)
cout<<"O";
else if(count==2)
cout<<" O "<<endl<<" | ";
else if(count==3)
cout<<" O "<<endl<<" /| ";
else if(count==4)
cout<<" O "<<endl<<" /|\ ";
else if(count==5)
cout<<" O "<<endl<<" /|\\ "<<endl<<" / ";
else if(count==6)
{
cout<<" O "<<endl<<" /|\\ "<<endl<<" / \\ ";
cout<<"You've lost!";
answer='*';
}
cout<<" "<<count;
cout<<endl;
if(secret==cover) //When all letters guessed program ends
{
cout<<"You guessed it!";
answer='*';
}
}
}while(answer!='*');
return(0);
}
-
You could try:
if(count >= 6) answer='*';
Somewhere at the end of your loop
-
Well the problem i was running into was that where i placed the count++; code it would not work out and add too many letters or just not add at all, so if I put it out of the for loop it will count every guess, I only want it to count the wrong guesses and I dont have anymore ideas of where to put count++, or any other ways of doing it.
-
if(wrong!=true) count++;
Shouldn't this be:
if(wrong==true) count++;
Since you only want to increase count when there have been a wrong answer...?
-
well wrong is just the word I use but the code in the loop checks if answer is in the word, and then makes wrong = true if it is
so if(wrong!=true) should work but it doesnt, if there is another way I can do this i will gladly change it because i havent been able to get it to work this way
-
Hmm, this might be the problem:
You never reset wrong to false at the beginning of each loop. So if you guess the right letter in say turn 3, wrong will have the value true in every loop after that one.
Set wrong to false in the beginning of your loop.
wrong=false;
(It might be more proper if you use the datatype bool on wrong, instead of int. bool can only have the value true and false)