Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a c++ program that looks like this: Enter phrase: hello. Phrase: -----. En

ID: 3784373 • Letter: W

Question

write a c++ program that looks like this:

Enter phrase: hello.

Phrase: -----.

Enter a guess: h

Phrase: h----.

Guessed so far: h

Wrong guesses left: 7

Enter a guess: e

Phrase: he---.

Guessed so far: he

Wrong guesses left: 7

Enter a guess: l

Phrase: hell-.

Guessed so far: hel

Wrong guesses left: 7

Enter a guess: d

Phrase: hell-.

Guessed so far: held

Wrong guesses left: 6

Enter a guess: l

Invalid guess! Please re-enter a guess: 8

Invalid guess! Please re-enter a guess: o

Phrase: hello.

Guessed so far: heldo

Wrong guesses left: 6

Congratulations!!

Notes:

(user input has been bolded and underlined for emphasis;
Note the two invalid guesses: ‘l’ is invalid because it is already in the list of previously guessed characters; and 8 is invalid because it is not alphabetic
Note also that the original non-alphabetic character ‘.’ was not replaced by a dash)

Explanation / Answer

PROGRAM CODE:

#include <iostream>
using namespace std;

string phrase, guessedPhrase, guessedLetters;
int length, wrongGuesses = 7;
char guess = '-';

void replacePhrase()
{
   for(int i=0; i<length; i++)
   {
       if(isalpha(phrase.at(i)))
           guessedPhrase += guess;
       else
           guessedPhrase += phrase.at(i);
   }
}
int main() {
  
   cout<<"Enter phrase: "<<endl;
   cin>>phrase;
   length = phrase.length();
   replacePhrase();
   cout<<"Phrase: "<<guessedPhrase<<endl<<endl;
   cout<<"Enter a guess: ";
   while(wrongGuesses > 0)
   {
       cin>>guess;
       string temp = phrase;
       if(guessedLetters.find(guess) == -1 && !isdigit(guess))
       {
           if(temp.find(guess) == -1)
           {
               wrongGuesses--;
           }
           else
           {
               guessedLetters += guess;
               while(temp.find(guess) != -1)
               {
                   int pos = temp.find(guess);
                   guessedPhrase.at(pos) = guess;
                   temp.at(pos) = '*';
               }
              
              
           }
           cout<<"Phrase: "<<guessedPhrase<<endl<<endl;
           cout<<"Guessed so far: "<<guessedLetters<<endl<<endl;
           cout<<"Wrong guesses left: "<<wrongGuesses<<endl<<endl;
           if(guessedPhrase == phrase)
           {
               cout<<"Congratulations!!";
               return 0;
           }
           cout<<"Enter a guess: ";
       }
       else cout<<" Invalid guess! Please re-enter a guess: ";
   }
   cout<<"Sorry You ran out of guesses !"<<endl<<"Game Over";
   return 0;
}

OUTPUT:
RUN #1

RUN #2