I am developing a Hangman game for an Uni assessment in C++ and I am having trouble displaying my hidden words after the user types a letter. So I have got the word being displayed as '_ _ _ _ _ _ _' but when I type a letter it doesn't swap the underscore for the actual letter.
game::game() {
words[0] = "strongly";
words[1] = "cheese";
words[2] = "computer";
words[3] = "coffee";
words[4] = "potatoes"; //words that can be in the game
words[5] = "zebra";
words[6] = "extinguisher";
words[7] = "solution";
words[8] = "diligent";
words[9] = "flabbergasted";
numGuesses = 0;
hiddenWord = words[rand() % 10]; //pick a random word from array words
completedWord = hiddenWord;
//for loop for changing the word to underscores
for (int i = 0; i < completedWord.length(); i++) {
completedWord[i] = '_';
}
//for loop adding a space after underscore
for (int i = 0; i < completedWord.length(); i++) {
cout << completedWord[i] << " ";
}
cout << endl;
cout << "Please enter a letter: ";
char guessedLetter;
cin >> guessedLetter;
if (guessedLetter = completedWord[0]) {
completedWord = guessedLetter;
//cout << guessedLetter << endl;
cout << completedWord << endl;
}
}
My whole program is separated into different header files and cpp files. So the code above is from my gameguesses.cpp and the header for that is below:
class game {
public:
string words[10];
game();
string hiddenWord;
int numGuesses;
string completedWord;
};
And this is what I actually get:
A help would be appreciated. Thank you!