Alright so i made my first hangman game but one problem i seem to be running in is that my validation check for "already used letters" dose not seem to be working it seems like the program just skips the validation code but i dont see why or how. Help would be appreciated.
The validation code for checking already used letters starts from line 36 if there is anything else let me know this is the code for the whole program btw.
// Hangman using functions and vectors Chapter 5 Exercise.
#include <iostream>
#include <string>
#include <cstdlib>
#include <list>
#include <ctime>
#include <vector>
#include <cctype>
#include <algorithm>
usingnamespace std;
// Max Try's for the game before the person is hanged.
constint maxGuesses = 10;
string playerGuess();
string guessCheck(string guess);
int main(){
guessCheck(playerGuess());
return 0;
}
string playerGuess()
{
string guess = "";
string used = "";
// Taking the players guess.
cout << "Please enter your guess in CAPITAL letters only!! thank you: ";
cin >> guess;
cout << endl;
// Checking if users guess has not already been used before.
while(used.find(guess) != string::npos){
cout << "You have already guessed " << guess << " before!" << endl;
cout << "Please enter a new guess: ";
cin >> guess;
}
used += guess;
return guess;
}
string guessCheck(string guess){
// Intiallizing vectors & variables.
vector<string> words;
words.push_back("INVINSIBLE");
words.push_back("DAMAGE");
words.push_back("BEAUTIFUL");
srand(time(0));
random_shuffle(words.begin(), words.end());
const string theWord = words[0];
int wrongChoice = 0;
string soFar(theWord.size(), '_');
cout << "\nSo far you have " << soFar << "\n\n";
// Checking players guess.
while(maxGuesses != wrongChoice && soFar != theWord){
if(theWord.find(guess) != string::npos){ // If the guess is correct.
cout << "Yes thats right! " << guess << " is in the word.\n";
// Updating words gussed so far.
for(int i = 0; i < theWord.length(); ++i){
if(theWord[i] == guess[0]){
soFar[i] = guess[0];
}
}
// Displaying words guess so far.
cout << "So far you have " << soFar << "\n\n";
if(soFar != theWord){
guess = playerGuess();
}
}
else{ // If user guess is wrong.
cout << endl << "Wrong Guess! \n";
wrongChoice++;
if(soFar != theWord){
guess = playerGuess();
}
}
}
// If the max tries have been reached.
if(maxGuesses == wrongChoice){
cout << "You have been hanged!! your lifeless body swings around as the crowd watches!!!" << endl << "the word was" << theWord << " by the way ;)";
}
else{ // If not then the player wins.
cout << "You guessed it!! you live to see another day yay!!!" << endl;
}
}