1) I need to make the game ask the player if they would like to play again and repeat if they choose yes. I have tried to add a loop in a few different ways but have not made any progress so far.

2) When a letter is guessed that is repeated in the hidden word only 1 letter is added. EX: Hidden word: difficult Guess letter: f Output: **f******

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.

    //Change the letters in the word to asterisks.
    int length = word.length();

    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.
        while (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 << " > " << letter << " ENTER ";
            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 2 Replies

Youve missed to top of your code

  1. The easiest way to put a repeat into a game once it is already working is to move the whole game into a function, then add your loop that askes the user if they want to repeat in main and call the function. A do ... while loop works well for this since you always want to play the first game.

  2. I'm slightly surprised that the loop at line 31 doesn't just do into an infinite loop when you guess a letter that is in the word. Line 34 - 36 actually replace a single correctly guessed letter in the word and are only done once for each letter guessed.

You code could use a proper refactor it is messy and confusing. I can tell this because the lines

    //Prompt the user to guess a character in the word.
    cout << "(Guess) Enter a letter in word " << word << " > ";
    cin >> letter;

Appear 3 times in your code. This should not be required, this program can easil be written with the guess request in only 1 place (I would say just inside the do ... while loop. The only other loop required would be to look for multiple occurances of the same letter that was guessed.

Thanks alot!! I got it working just like it was suppose to. I was having trouble making it just one loop but I will keep that in mind for future projects.

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.