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 variableint w = 0;// define number of words that need to be guessed by the user assume 4const int WORDS = 4;// loopdo{// we will define the hangman bodyconst char body[] = "o/|\\|\\";// here we define the wordsstring words[WORDS] = {"MACAW", "SADDLE", "TOASTER", "XENOICIDE"};// fetch size or lengthstring xword(words[w].length(),'*');// define iterator to fetch the wordsstring::iterator i, ix = xword.begin();// define number of words to prompt for the userchar letters[26]={"\0"};// Now we define the variables which will be used in the simulationint n =0, xcount = xword.length();bool found = false, solved = false, hung = false;int offset = 0, bodyCount = 0;// Game play iterationdo{char temp;// prompt for the charactercout<"guess a="" letter="" (here="" case="" doesn't="" matter):="">"guess><><"'*' denotes="" number="" of="" possible="">"'*'><>cin>>temp;// define validation of input lettersif(isalpha(temp)){cout<"please enter="" letters:="">"please><>}elsecontinue;// char caseletters[n]= toupper(temp);// now intialize the index variable over the character array's wordi = words[w].begin();// iterate until word's length endswhile(i != words[w].end()){// check the wordsif(*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 nowif(--xcount == 0)solved = true;}++i;++offset;}// if letter is not found then increase the body countif(!found)++bodyCount;bool newline = false;// now iterate over the incorrect counter to draw shape or hangmanfor(int q = 1; q<=bodycount;>=bodycount;>{// check for attemptsif(q==1 || q ==5 || q == 7){newline = true;cout<>else if(q==4){newline = true;}else{newline = false;}cout<>cout<>}// check for attempts if exceededif(bodyCount>=7){cout<><><"game>"game><><>hung = true;break;}// print user guess valuescout<><"guesses:>"guesses:><>for(int q=0;q<>{cout<>++n;}}while(!solved);cout<"word:>"word:><><>if(!hung)cout<><"congratulations!! you="">"congratulations!!><>/*if ( ++w >= WORDS )break;*/// prompt for respeonsecout<"want to="" play="" again?="" yes="" or="" no:="" ";cin="">>response;}while(!response.compare("yes"));cout<"thank>"thank><>return 0;}"want>
int main(){// define variable to get the response from user "Yes" or "No"string response;// Define index variableint w = 0;// define number of words that need to be guessed by the user assume 4const int WORDS = 4;// loopdo{// we will define the hangman bodyconst char body[] = "o/|\\|\\";// here we define the wordsstring words[WORDS] = {"MACAW", "SADDLE", "TOASTER", "XENOICIDE"};// fetch size or lengthstring xword(words[w].length(),'*');// define iterator to fetch the wordsstring::iterator i, ix = xword.begin();// define number of words to prompt for the userchar letters[26]={"\0"};// Now we define the variables which will be used in the simulationint n =0, xcount = xword.length();bool found = false, solved = false, hung = false;int offset = 0, bodyCount = 0;// Game play iterationdo{char temp;// prompt for the charactercout<"guess a="" letter="" (here="" case="" doesn't="" matter):="">"guess><><"'*' denotes="" number="" of="" possible="">"'*'><>cin>>temp;// define validation of input lettersif(isalpha(temp)){cout<"please enter="" letters:="">"please><>}elsecontinue;// char caseletters[n]= toupper(temp);// now intialize the index variable over the character array's wordi = words[w].begin();// iterate until word's length endswhile(i != words[w].end()){// check the wordsif(*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 nowif(--xcount == 0)solved = true;}++i;++offset;}// if letter is not found then increase the body countif(!found)++bodyCount;bool newline = false;// now iterate over the incorrect counter to draw shape or hangmanfor(int q = 1; q<=bodycount;>=bodycount;>{// check for attemptsif(q==1 || q ==5 || q == 7){newline = true;cout<>else if(q==4){newline = true;}else{newline = false;}cout<>cout<>}// check for attempts if exceededif(bodyCount>=7){cout<><><"game>"game><><>hung = true;break;}// print user guess valuescout<><"guesses:>"guesses:><>for(int q=0;q<>{cout<>++n;}}while(!solved);cout<"word:>"word:><><>if(!hung)cout<><"congratulations!! you="">"congratulations!!><>/*if ( ++w >= WORDS )break;*/// prompt for respeonsecout<"want to="" play="" again?="" yes="" or="" no:="" ";cin="">>response;}while(!response.compare("yes"));cout<"thank>"thank><>return 0;}
"want>
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here