Working on another school assignment! This one involves replacing blocks of code with functions that perform the same task. I created the function asking for user input easily enough, but the function I created to verify that input seems to be causing problems.
//Hangman
// The classic game of hangman
//Chapter 5 HW, will be modifying this code
//write function to get player guess
//write function to determine if the guess is in the secret word
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
void playerGuess();
void playerCorrect();
usingnamespace std;
int main()
{
// set-up
constint MAX_WRONG = 8; // maximum number of incorrect guesses allowed
vector<string> words; // collection of possible words to guess
words.push_back("GUESS");
words.push_back("HANGMAN");
words.push_back("DIFFICULT");
srand(static_cast<unsignedint>(time(0)));
random_shuffle(words.begin(), words.end());
const string THE_WORD = words[0]; // word to guess
int wrong = 0; // number of incorrect guesses
string soFar(THE_WORD.size(), '-'); // word guessed so far
string used = ""; // letters already guessed
cout << "Welcome to Hangman. Good luck!\n";
// main loop
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: ";
playerGuess(); //replacement of code with function
guess = toupper(guess); //make uppercase since secret word in uppercase
while (used.find(guess) != string::npos)
{
cout << "\nYou've already guessed " << guess << endl;
cout << "Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}
used += guess;
playerCorrect(); //replacement of code with function
}
// shut down
if (wrong == MAX_WRONG)
cout << "\nYou've been hanged!";
else
cout << "\nYou guessed it!";
cout << "\nThe word was " << THE_WORD << endl;
return 0;
}
//This function will take the users input
void playerGuess()
{
char guess;
cin >> guess;
}
//This function will test if the player's guess is in the secret word
void playerCorrect()
{
constint MAX_WRONG = 8;
vector<string> words;
words.push_back("GUESS");
words.push_back("HANGMAN");
words.push_back("DIFFICULT");
srand(static_cast<unsignedint>(time(0)));
random_shuffle(words.begin(), words.end());
const string THE_WORD = words[0];
int wrong = 0;
string soFar(THE_WORD.size(), '-');
string used = "";
char 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 (unsignedint 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;
}
}
Your function playerGuess receives input, and does nothing with it. Doesn't return it. Doesn't store it anywhere. The function finishes, the input is lost.
That seems to have worked, though I think there is still something wrong with the other function. I think it's a logic error, but I just don't quite get it. I can't visualize the solution
If you need a hint on how to say what the problem is, think about what you want the program to do that it doesn't do, and say that. Alternatively, think about what the program does that you don't want it to do, and say that.
I think the problem lies within the playerCorrect function. I think the guess value might be where the problem lies.
https://imgur.com/a/8oNsp This is the program running. There is a strange blank spot where a letter should be, which leads me to believe that there's something wrong with 'guess'. There's also something wrong with the verification of whether or not the letters are correct, as none of the letters seem to be considered correct.
char guess;
if (THE_WORD.find(guess) != string::npos)
What do you think the value of guess is in that second line? Given that it is created in that first line there, and never set to anything, what do you expect its value to be?