Hello, I was making a hangman simulation in C++. The code runs, but not fully. Could you identify the error and fix it?   #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 i, ix = xword.begin(); // define number of words to prompt for the user char letters[26]={"\0"}; // Now we define the variables which will be used in the simulation int n =0, xcount = xword.length(); 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; // 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; }while(!response.compare("yes")); cout<<"thank you!!"<

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Hello,

I was making a hangman simulation in C++.

The code runs, but not fully.

Could you identify the error and fix it?

 

#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 i, ix = xword.begin();
// define number of words to prompt for the user
char letters[26]={"\0"};
// Now we define the variables which will be used in the simulation
int n =0, xcount = xword.length();
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;
// define validation of input letters
if(isalpha(temp))
{
cout<<"Please Enter letters: "<<endl;
}
else
continue;

// char case
letters[n]= toupper(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[n])
{
// 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;
}
// 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;
}
// print user guess values
cout<<endl<<"Guesses: "<<endl;
for(int q=0;q<=n;q++)
{
cout<<letters[q];
++n;
}
}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;
}while(!response.compare("yes"));

cout<<"thank you!!"<<endl;
return 0;
}

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY