2

So i want to make a simple hangman console game that chooses random words from a file. The problem is, when i try to check if the input is found in the word i'm trying to guess, the program jumps over the check. I'm thinking i made a serious mistake earlier in another functions and i don't know how to fix it. Below you can find the first function:

#include <string> #include <iostream> int main() { std::string CorrectStrMemo = "hello"; std::string TemporaryStrMemo = "h---o"; std::string WordToGuess; WordToGuess = TemporaryStrMemo; std::cout << WordToGuess << std::endl; int lives; if (WordToGuess.length() < 4) lives = 5; else lives = WordToGuess.length() / 2 + 3; std::cout << "\nYou have " << lives << " lives!"; char UserGuess; std::cout << "(" << CorrectStrMemo << ")"; while (lives > 0 && WordToGuess != CorrectStrMemo) { std::cout << "\nGuess the letter : "; std::cin >> UserGuess; for (size_t i = 1; i < WordToGuess.length() - 1; i++) //works fine for small words or 3 letter words, with 1 unknown one { for (size_t j = 1; j < CorrectStrMemo.length() - 1; j++) { if (lives > 0 && CorrectStrMemo[j] == UserGuess) { std::cout << "\nCorrect guess! Keep going!"; WordToGuess[i] = UserGuess; std::cout << "\nUpdated word is: " << WordToGuess; std::cout << "\nLives remaining: " << lives; break; } else { std::cout << "\nIncorrect guess!" << std::endl; lives--; std::cout << lives << " lives remaining. Use them wisely!" << std::endl; std::cout << " " << WordToGuess; break; } break; } } //conditions to break the while and display the end screeens if (WordToGuess == CorrectStrMemo && lives > 0) { std::cout << std::endl << "You won!"; break; } else if (lives == 0) { std::cout << "You have no lives left to play!" << std::endl //need to connect it with the win/lose screeens << "The game is over!"; break; } } } 

Sorry for pasting all the code, but i don't know where is the flaw so i thought is better to do it this way. Thanks in advance!

3
  • Fyi, this : while (!WordsFile.eof()) is wrong. See this for why. Further, I strongly advise you not presize the vector, then fill it by subscripting. Start with an empty vector and use push_back (or emplace_back) to ensure an accurate size (which also benefits from no longer needing WordsCounter). Commented Jan 30, 2022 at 11:24
  • I've added a more minimal reproducible example, does that still reproduce your problem? If not feel free to revert my edit and add your own minimal reproducible example Commented Jan 30, 2022 at 11:28
  • @AlanBirtles Thanks for the edit. It does reproduce the problem. Didn't knew about minimal reproducible example. Commented Jan 30, 2022 at 11:44

1 Answer 1

2

I'm not sure why you have two for loops? The inner for loop checks whether the first letter matches, if it does then it prints correct and breaks, if it isn't then it prints incorrect and breaks so in both cases only the first letter is checked. The outer loop then runs again and again the inner loop checks the first letter. In the second iteration of the outer loop the inner loop again checks the first letter, if it was correct then it sets the second letter of the answer to the guessed character. If it was incorrect it uses up another life. I think you only need one for loop and your code can be simplified to:

while (lives > 0 && WordToGuess != CorrectStrMemo) { std::cout << "\nGuess the letter : "; std::cin >> UserGuess; bool correct = false; for (size_t i = 1; i < CorrectStrMemo.length() - 1; i++) { if (CorrectStrMemo[i] == UserGuess) { correct = true; WordToGuess[i] = UserGuess; } } if (correct) { std::cout << "\nCorrect guess! Keep going!"; std::cout << "\nUpdated word is: " << WordToGuess; std::cout << "\nLives remaining: " << lives; } else { std::cout << "\nIncorrect guess!" << std::endl; lives--; std::cout << lives << " lives remaining. Use them wisely!" << std::endl; std::cout << " " << WordToGuess; } //conditions to break the while and display the end screeens if (WordToGuess == CorrectStrMemo && lives > 0) { std::cout << std::endl << "You won!"; break; } else if (lives == 0) { std::cout << "You have no lives left to play!" << std::endl //need to connect it with the win/lose screeens << "The game is over!"; break; } } 

Now it checks the guessed character against every character in the answer and only prints once per guess whether it was right or wrong.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.