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;
}

Recommended Answers

All 5 Replies

Also I just noticed If I enter a wrong answer twice the program doesn't continue on.

While this is a pretty short program, I think it would still help to break it down in to functions. Some ideas for functions would be:

void GuessLetter(char letter); //the main logic - every user input should trigger this function
bool ContainsLetter(string word, char letter); //determine if 'letter' is contained in 'word'
void ReplacePlaceholder(string &word, char letter); //replace an '*' with 'letter'
bool IsDone();

Those are just examples, but the point is that then your code is very readable, like this:

do
{
  cin >> letter;
  GuessLetter(letter);
}while(!IsDone())

void GuessLetter(char letter)
{
  if(ContainsLetter(word, letter)
  {
  ReplacePlaceholder(word, letter);
  }
}

Good luck,

David

Thank you David. I will try this but the only problem I am having is that this assignment is due tomorrow and I have been working on this all day (along with another one dealing with anagrams) but this one has been my main problem. I feel like I am so close to an answer I just cannot figure out why it won't replace more than one asterisk with the same letter and why I cannot miss more than one guess. I feel like under the time constraints I would be better off to just try and fix the code I have unless the code I have is so far from the right answer I should just forget about it.

I promise I am not TRYING to be a pain.

Line 41 - It looks to me like you have an "if" statement instead of a while statement when you are replacing the correctly-guessed letters. You need a while statement if you want to replace ALL the instances of a letter You also appear to be missing a closing bracket on line 49 and have an extra one on line 67:

while(/* game not over */)
{
    // ask for a guess (i.e. 'c')
    // change lines 41 to 49 with something like below
    while (/* there are more c's to replace */)
    {
        // find and replace the next instance of 'c' - i.e. replace '*' with 'c'
    }
}

You can use the "find" command, as you do, or you can use a loop and go through the word character by character, testing and replacing if necessary. Regardless, you need a loop (either for or while), not an if-statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.