I am trying to finish this hangman game I started. I know how to do everything I have left to do except for one thing. When I run the program and start guessing letters for instance in the word technical, if I guess the letter c, it only puts one c into the word and the other c is never copied into the word and it makes the game impossible to finish. Can anyone help me figure out what I am leaving out? By the way the lines that are commented delete are just lines I use to see the word when I am testing the program. They won't be in the final program. :) Thanks.
Code:
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
//Hangman Game.
//Guess the secret word by guessing letters of the word.
int main ()
{
srand(time (0) ); //seed for the random function.
string words[5] = {"programming", "technical", "computer", "language", "difficult"};//Words I chose.
string word; //string for the word with asterisks.
word = words[rand() % 5]; //Randomly chooses from the lists of words I provided.
//Create a temporary holder for the word without asterisks.
string temporary;//string for the original word without the asterisks. Used for searching for characters.
temporary = word;//Makes the temporary string equal to the word before the asterisks are placed there.
cout << temporary << endl;//LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK DELETE!!!!
//Change the letters in the word to asterisks.
int length = word.length();
cout << word << endl;//LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK LOOK DELETE!!!!!!(This is for me to see the word during
//testing of the program.)
for(int i=0; i<(int)word.length(); i++ )
{
word[i]='*';
}
//Prompt the user to guess a character in the word.
cout << "(Guess) Enter a letter in word " << word << " > ";
string letter;
cin >> letter;
//See if the letter entered matches any of the letters in the chosen word. If so the asterisks need to be replaced with
//the actual letter.
//I will use the temporary string to compare my answers and then correct the word as the game progresses.
//A wrong guess counter needs to start here and be incremented every time a wrong guess is made.
//Placeholder is simply to keep the loop running.
int wrongGuess = 0;
string placeholder;
placeholder = word;
do
{
//Checks to make sure that the letter is part of the word.
if (temporary.find(letter) != string::npos)
{
//If the letter is part of the word the letter is put in the place of the asterisk in the correct location.
word.replace(temporary.find(letter), 1, letter);
//Then part of the placeholder is erased.
placeholder.erase(1,1);
//The user is then promted to enter another word.
cout << "(Guess) Enter a letter in the word " << word << " > ";
cin >> letter;
//If the letter entered is not part of the word...
if (temporary.find(letter) == string::npos)
{
//The wrong guess counter is incremented.
wrongGuess++;
//A message is displayed.
cout << letter << " is not part of the word!" << endl;
//And the user is prompted to enter another word.
cout << "(Guess) Enter a letter in the word " << word << " > ";
cin >> letter;
}
//If the letter has already been guessed...
if (word.find(letter) != string::npos)
{
//A message is displayed
cout << letter << " is already part of the word." << endl;
}
}
}
//All of this is done until the placeholder is empty.
while (placeholder.empty() == false);
//When the placeholder word becomes empty the game is over and the word has been guessed.
//Display a message to the user to show that the word has been guess and how many missed guesses there were.
if (placeholder.empty() == true)
cout << "You guessed the word!!! The word is " << word << ".";
cout << "You missed " << wrongGuess << " time(s)." << endl;
return 0;
}