简体   繁体   中英

Is there a method on how i can find and replace a character in a string?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM