i have been given an assignment my my university to make a hangman game, and for the most part i have done that, unfortunately i am having a lot of issues with my guess checking, for some reason (probably due to my terrible programming) it only checks the first letter of the word against the guessed letter, is there anyway to fix my code.
please be patient with me i haven't been doing this all that long and its been a very long day
while (lives > 0)
{
std::cout<< word; // help with testing (so i can see what word has been picked)
std::cout<<"\t \t please enter your guess"<<std::endl;
std::cin>> guess;
for(int i = 0; i < strlen(word); i++) //checking letter input against the word
{
int c = word[i];
if(c == 0)
{
break;
}
if (guess == c)
{
std::cout<<"that is correct"<<std::endl;
std::cout<<"you have"<<lives<<"remaining"<<std::endl;
break;
}
else
{
std::cout<<" that is incorrect"<<std::endl;
std::cout<<"you have"<<lives<<"remaining"<<std::endl;
break;
}
}
}
if ( c == 0 ) .it should be if ( c == '0' )
if the first two statement is false then the else statement will be executed which has a break statement which will stop the loop.
in others word, if the guess didnt hit the first letter of the word the loop will be stopped.
try something like this.