Hello, I'm working on this hangman simulation in C++. The code runs but it doesn't count wrong guesses after one letter is guessed correctly. Also, the body of the hangman comes out wrong. Could you help?         #include #include // for string class functions #include #include using namespace std; int main() {     // define variable to get the response from user "Yes" or "No"     string response;     // Define index variable     int w = 0;     // define number of words that need to be guessed by the user assume 4     const int WORDS = 4;          // loop     do     {         // we will define the hangman body         const char body[] = "o/|\\|\\";         // here we define the words         string words[WORDS] = {"MACAW", "SADDLE", "TOASTER", "XENOICIDE"};         // fetch size or length         string xword(words[w].length(),'*');         // define iterator to fetch the words         string::iterator ix = xword.begin();         string::iterator i = xword.begin();         // define number of words to prompt for the user         string letters = "";         // Now we define the variables which will be used in the simulation         int n =0, xcount = xword.length(), k=0;         bool found = false, solved = false, hung = false;         int offset = 0, bodyCount = 0;                  // Game play iteration         do         {             char temp;             // prompt for the character             cout<<"Guess a letter (Here case doesn't matter): "<>temp;             temp = toupper(temp);             // define validation of input letters             if(isalpha(temp))             {                 cout<<"Please enter letters: "<=7)             {                 cout<= WORDS )         break;*/         // prompt for respeonse         cout<<"Want to play again? yes or no: ";         cin>>response;         if(response.compare("yes")==0)             w++;     }while(!response.compare("yes"));          cout<<"thank you!!"<

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Hello, I'm working on this hangman simulation in C++. The code runs but it doesn't count wrong guesses after one letter is guessed correctly. Also, the body of the hangman comes out wrong. Could you help?

 

 

 

 

#include<iostream>
#include<cstring> // for string class functions
#include<fstream>
#include <cctype>
using namespace std;

int main()
{
    // define variable to get the response from user "Yes" or "No"
    string response;
    // Define index variable
    int w = 0;
    // define number of words that need to be guessed by the user assume 4
    const int WORDS = 4;
    
    // loop
    do
    {
        // we will define the hangman body
        const char body[] = "o/|\\|\\";
        // here we define the words
        string words[WORDS] = {"MACAW", "SADDLE", "TOASTER", "XENOICIDE"};
        // fetch size or length
        string xword(words[w].length(),'*');
        // define iterator to fetch the words
        string::iterator ix = xword.begin();
        string::iterator i = xword.begin();
        // define number of words to prompt for the user
        string letters = "";
        // Now we define the variables which will be used in the simulation
        int n =0, xcount = xword.length(), k=0;
        bool found = false, solved = false, hung = false;
        int offset = 0, bodyCount = 0;
        
        // Game play iteration
        do
        {
            char temp;
            // prompt for the character
            cout<<"Guess a letter (Here case doesn't matter): "<<xword<<". '*' denotes number of possible characters"<<endl;
            cin>>temp;
            temp = toupper(temp);
            // define validation of input letters
            if(isalpha(temp))
            {
                cout<<"Please enter letters: "<<endl;
            }
            else
                continue;
            
            // char case
            letters = letters + temp ;
            // now intialize the index variable over the character array's word
            i = words[w].begin();
            // iterate until word's length ends
            while(i != words[w].end())
            {
                // check the words
                if(*i == letters.at(k))
                {
                    // it is used for masking purpose. letter is found then masked it.
                    *(ix + offset) = *i;
                    found = true ;
                    // If a letter is found then move to the next character and in final let us know word is solved now
                    if(--xcount == 0)
                    solved = true;
                }
                ++i;
                ++offset;
            }
            offset = 0;
            k++;
            // if letter is not found then increase the body count
            if(!found)
                ++bodyCount;
            
            bool newline = false;
            
            // now iterate over the incorrect counter to draw shape or hangman
            for(int q = 1; q<=bodyCount; ++q)
            {
                // check for attempts
                if(q==1 || q ==5 || q == 7)
                {
                    newline = true;
                    cout<<body[0];
                }
                else if(q==4)
                {
                    newline = true;
                }
                else
                {
                    newline = false;
                }
                cout<<body[q];
                if(newline)
                    cout<<endl;
            }
            // check for attempts if exceeded
            if(bodyCount>=7)
            {
                cout<<endl<<endl<<"GAME OVER"<<endl<<endl;
                hung = true;
                break;
            }
            cout<<endl<<"Guesses: "<< letters <<endl;
        }while(!solved);
        cout<<"Word: "<<words[w]<<endl;
        
        if(!hung)
        cout<<endl<<"Congratulations!! you guessed"<<endl;
        /*if ( ++w >= WORDS )
        break;*/
        // prompt for respeonse
        cout<<"Want to play again? yes or no: ";
        cin>>response;
        if(response.compare("yes")==0)
            w++;
    }while(!response.compare("yes"));
    
    cout<<"thank you!!"<<endl;
    return 0;
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education