It is necessary to make a function that will accept its variant from the user, as well as another function that will determine whether the user variant matches the word that has been made.

I don’t fully understand the topic of functions. The code should generate a random word from a vector and offer to guess it. The user enters a letter or expression and then the code checks the entered expression and performs a specific action (says that this character has already been entered, this character is not in the word). If the user makes a mistake 8 times, he loses and he is offered to play again. But instead, the program skips this part of the code.

while ((wrong < MAX_WRONG) && (soFar != THE_WORD)) { cout << "\n\nYou have " << (MAX_WRONG - wrong); cout << " incorrect guesses left.\n"; cout << "\nYou've used the following letters:\n" << used << endl; cout << "\nSo far, the word is:\n" << soFar << endl; char enter_guess(string); int check_guess(vector<string>words); } 

CODE:

  #include "pch.h" #include <iostream> #include <string> #include <vector> #include <algorithm> #include <ctime> #include <cctype> using namespace std; char guess; int wrong = 0; const string THE_WORD; string soFar; string used; char enter_guess(); int check_guess(vector<string>words); int main() { const int MAX_WRONG = 8; vector<string> words; words.push_back("GUESS"); words.push_back("HANGMAN"); words.push_back("DIFFICULT"); string answer; cout << "Welcome to Hangman. Good luck!\n"; while ((answer != "No") || (answer != "no")) { while ((wrong < MAX_WRONG) && (soFar != THE_WORD)) { cout << "\n\nYou have " << (MAX_WRONG - wrong); cout << " incorrect guesses left.\n"; cout << "\nYou've used the following letters:\n" << used << endl; cout << "\nSo far, the word is:\n" << soFar << endl; char enter_guess(string); int check_guess(vector<string>words); } if (wrong == MAX_WRONG) cout << "\nYou've been hanged!"; else cout << "\nYou guessed it!"; cout << "\nThe word was " << THE_WORD << endl; cout << "\nDo you want to play again?(Enter: Yes or No) " << endl; cin >> answer; } return 0; } char enter_guess() { cout << "\n\nEnter your guess: "; cin >> guess; guess = toupper(guess); return guess; } int check_guess(vector<string>words) { srand(static_cast<unsigned int>(time(0))); random_shuffle(words.begin(), words.end()); const string THE_WORD = words[0]; string soFar(THE_WORD.size(), '-'); string used = ""; while (used.find(guess) != string::npos) { cout << "\nYou've already guessed " << guess << endl; cout << "Enter your guess: "; cin >> guess; guess = toupper(guess); } used += guess; if (THE_WORD.find(guess) != string::npos) { cout << "That's right! " << guess << " is in the word.\n"; // update soFar to include newly guessed letter for (unsigned int i = 0; i < THE_WORD.length(); ++i) { if (THE_WORD[i] == guess) { soFar[i] = guess; } } } else { cout << "Sorry, " << guess << " isn't in the word.\n"; ++wrong; } return wrong; } 

Here is the code but without the use of functions. And here the code works. So what's the problem?

 #include "pch.h" #include <iostream> #include <string> #include <vector> #include <algorithm> #include <ctime> #include <cctype> using namespace std; int main() { const int MAX_WRONG = 8; vector<string> words; words.push_back("GUESS"); words.push_back("HANGMAN"); words.push_back("DIFFICULT"); string answer; while ((answer!="No")||(answer!="no")) { srand(static_cast<unsigned int>(time(0))); random_shuffle(words.begin(), words.end()); const string THE_WORD = words[0]; int wrong = 0; string soFar(THE_WORD.size(), '-'); string used = ""; cout << "Welcome to Hangman. Good luck!\n"; while ((wrong < MAX_WRONG) && (soFar != THE_WORD)) { cout << "\n\nYou have " << (MAX_WRONG - wrong); cout << " incorrect guesses left.\n"; cout << "\nYou've used the following letters:\n" << used << endl; cout << "\nSo far, the word is:\n" << soFar << endl; char guess; cout << "\n\nEnter your guess: "; cin >> guess; guess = toupper(guess); while (used.find(guess) != string::npos) { cout << "\nYou've already guessed " << guess << endl; cout << "Enter your guess: "; cin >> guess; guess = toupper(guess); } used += guess; if (THE_WORD.find(guess) != string::npos) { cout << "That's right! " << guess << " is in the word.\n"; // update soFar to include newly guessed letter for (unsigned int i = 0; i < THE_WORD.length(); ++i) { if (THE_WORD[i] == guess) { soFar[i] = guess; } } } else { cout << "Sorry, " << guess << " isn't in the word.\n"; ++wrong; } } if (wrong == MAX_WRONG) cout << "\nYou've been hanged!"; else cout << "\nYou guessed it!"; cout << "\nThe word was " << THE_WORD << endl; cout << "\nDo you want to play again?(Enter: Yes or No) " << endl; cin >> answer; } return 0; } 
  • 2
    This code already contains two functions besides main - VTT
  • Describe what it means "incorrectly" in the question with the help of the "edit" button. Indicate what you expect and what the current code gives - cpp questions
  • It seems that you specifically try to write more and longer. - AR Hovsepyan
  • Why did you try to 'clear' the question? - HolyBlackCat

1 answer 1

You try to look for some character in the empty line .. when there is such an error:

 string used = ""; while (used.find(guess) != string::npos) { //... 

You can not look at the rest of the code.

It is because of this error you do not get the result. Another thing is that the program itself is logically incorrect. So this is the answer, although it may seem incomplete.

  • four
    Here I do not understand some people. The man was pointed to a completely blatant blooper, and instead of saying thanks and correcting, he begins to swing right. - freim 7:27 pm
  • one
    @Dinis Satvalov, you have a mistake here, and in many places. In addition, you have the wrong approach and extra writings. If you called the object used _ om, it does not mean that this object contains the word. - AR Hovsepyan
  • @Kromster, because of this error, the author does not get the result. Another thing is that the program itself is logically incorrect. So this is just the answer, although it may seem incomplete ... - AR Hovsepyan
  • Completed your answer, check pliz, is everything so? Comments can be deleted. - Kromster