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

c++ program: write a program to create a hangman game.. { char guessedletters[26

ID: 3834439 • Letter: C

Question

c++ program:

write a program to create a hangman game..

{
   char guessedletters[26] = { '' }; //sets all guessed letters to null
   int guessleft = 6;
   int lenw; //length of actual word
   int guessed = 0; //if set to 1 then quit
   int i, x, xflag, winflag = 0;
   char userguess = ''; //the current letter guessed by the user
   char guessedword[20];

   //The guessedword contains the word as the user
   //knows it at the current time. So if they
   //guessed one correct letter, it will show the one
   //letter in the correct location. It is set to
   //underscores so that it shows the user which
   //letters are still missing.
   for (x = 0; x<20; ++x)
       guessedword[x] = '_';

   //The secretword is a global variable defined
   //at the top of the program. This means you
   //don't have to pass it to any other functions.
   //You don't have to use a secretword. You can
   //have an array with a bunch of secret words and
   //the computer randomly picks a word (recommended)
   //or you can have a second user enter a word.
   //Either way, leave it like this until you get
   //it working and then change it to one of the
   //above cases if you like.
   lenw = secretword.length(); //secretword is defined at top of program

                               //*ADD a WHILE LOOP here while the user has not guessed the
                               //*word AND (&&) while you still have guesses left (guessleft is above 0)
   {

       drawing(guessleft); //This draws the first hangman picture
       cout << "Guessed letters: ";
       //***Insert a for loop to print out the current list
       //***of guessed letters - that is, print out the array
       //***guessedletters
       cout << endl;

       cout << "This is what you have: " << endl;
       for (x = 0; x            cout << guessedword[x] << " ";
       cout << endl << endl;


       //***Go through the guessedword array
       //***to see if it has been completely guessed
       //*** and if so, tell user they won and break out
       //*** or else if user did not win,
       //*** make sure winflag is set back to 0
       //*** and continue on. Do it any way you like,
       //*** but winflag=1 if they guessed word and
       //*** winflag=0 if they have not.


       //***Get user guess and save it in the
       //***variable userguess


       xflag = 0;
       for (i = 0; i<26; ++i) //User has 26 tries with letters
       {
           if (userguess == guessedletters[i])
           {
               cout << endl << "You already guessed this letter." << endl;
               system("pause");
               break;
           }
           else if (guessedletters[i] == '') //this means the letter wasn't guessed yet
           {
               guessedletters[i] = userguess; //so add userguess as a guessed letter

                                           //***go through and see if the userguess is anywhere in
                                           //***secretword, and if so, update the guessedword array
                                           //***with the letter. You will also need to
                                           //***update the guessleft variable (decrement it) if
                                           //***they haven't guessed correctly. If they did guess
                                           //***a correct letter than don't decrement guessleft since
                                           //***they don't lose a try if they guessed correctly.

           }
       }

   }

   cout << endl << "Game finished..." << endl;
   if (winflag == 0)
   {
       drawing(0);
       cout << "Sorry. Better luck next time!" << endl;
   }

Explanation / Answer

Here is the C++ program for the hangman game:

#include <iostream>

#include <string>

using namespace std;

int main()

{

cout << "Enter the word for other player to guess" << endl;

string word;

getline(cin, word);

string copy = word;

string Underscore;

for(int i=0; i!=word.length(); i++)

{

if(word.at(i) == ' ')

{

Underscore += " ";

} else{

Underscore += "_";

}

}

for(int i=0; i!=50; ++i)

{

cout << endl;

}

string guess;

int wrong=0;

while(1){

if(wrong == 6){

cout << "You Lose! The word was: " << word << endl;

break;

}

cout << Underscore << endl;

cout << "There are " << word.length() << " letters with spaces" << endl;

cout << "You have " << 6 - wrong << " more tries left" << endl;

if(Underscore == word){

cout << "You win!" << endl;

break;

}

cout << "Guess a letter or a word" << endl;

getline(cin, guess);

if(guess.length() > 1)

{

if(guess == word)

{

cout << "That's right, you win!" << endl;

break;

}

else

{

cout << "wrong word " << endl;

wrong++;

}

}

else if(copy.find(guess) != -1){

while(copy.find(guess) != -1)

{

Underscore.replace(copy.find(guess), 1, guess);

copy.replace(copy.find(guess), 1, "_");

}

}

else

{

cout << "That's wrong" << endl;

wrong++;

}

cout << endl;

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote