Hello, This is part of my hangman simulation in C++. If you compile and run it and type "Easy," the code should run. If you run it though, the body of the hangman doesn't align when you guess wrong....


Hello, This is part of my hangman simulation in C++.


If you compile and run it and type "Easy," the code should run.


If you run it though, the body of the hangman doesn't align when you guess wrong. Could you help me with that and implement an if statement to repeat the program if user wants to play again?






#include


#include


#include


#include


#include
using namespace std;


const int MAX_TRIES = 5;
char answer;


int letterFill(char, string, string&);




int main()


{


string name;


char letter;


int num_of_wrong_guesses = 0;


string word;



srand(time(NULL)); // ONLY NEED THIS ONCE!



// welcome the user


cout < "\n\nwelcome="" to="" hangman!!="" guess="" a="" fruit="" that="" comes="" into="" your="">


// Ask user for for Easy, Average, Hard


string level;


cout < "\nchoose="" a="" level(e="" -="" easy,="" a="" -="" average,="" h="" -="" hard):"=""><>


cin >> level;



// compare level


if (level == "Easy")


{
//put all the string inside the array here


string easy[] = { "grape", "apple", "banana", "strawberry" };


string word;



int n = rand() % 4;


word = easy[n];



//call the function here for guessing game


// Initialize the secret word with the * character.


string unknown(word.length(), '*');


cout < "\n\neach="" letter="" is="" represented="" by="" an="">


cout < "\n\nyou="" have="" to="" type="" only="" one="" letter="" in="" one="">


cout < "\n\nyou="" have="" "="">< max_tries="">< "="" tries="" to="" try="" and="" guess="" the="">


cout <>


// Loop until the guesses are used up


while (num_of_wrong_guesses <>


{


cout < "\n\n"=""><>


cout < "\n\nguess="" a="" letter:="">


cin >> letter;


// Fill secret word with letter if the guess is correct,


// otherwise increment the number of wrong guesses.


if (letterFill(letter, word, unknown) == 0)


{


cout < endl="">< "whoops!="" that="" letter="" isn't="" in="" there!"=""><>


num_of_wrong_guesses++;
if(num_of_wrong_guesses==1){
cout<" o=""><>
}
if(num_of_wrong_guesses==2){
cout<" o=""><>
cout<>
}
if(num_of_wrong_guesses==4){
cout<" \t="" o=""><>
cout< "'\\'="">
cout<" |=""><>
cout<"' '=""><>
}
if(num_of_wrong_guesses==3){
cout<"\t o=""><>
cout<"'\\'><>
cout<" |=""><>
}
}


else


{


cout < endl="">< "you="" found="" a="" letter!="" isn't="" that="" exciting?"=""><>


}


// Tell user how many guesses has left.


cout < "you="" have="" "="">< max_tries="" -="">


cout < "="" guesses="" left."=""><>


// Check if user guessed the word.
if (word == unknown)


{


cout < word=""><>


cout < "yeah!="" you="" got="">


break;


}


}


if (num_of_wrong_guesses == MAX_TRIES)


{


cout < "\nsorry,="" you="" lose...you've="" been="" hanged."=""><>


cout < "the="" word="" was="" :="" "="">< word=""><>
cout<><"\t o=""><>
cout<" '\\'=""><>
cout<><"\t |=""><>
cout<"' '="" '\\'=""><>


}


cin.ignore();


cin.get();


return 0;


}


}
int letterFill(char guess, string secretword, string &guessword)


{


int i;


int matches = 0;


int len = secretword.length();


for (i = 0; i< len;="">


{


// Did we already match this letter in a previous guess?


if (guess == guessword[i])


return 0;


// Is the guess in the secret word?


if (guess == secretword[i])


{


guessword[i] = guess;


matches++;


}


}


return matches;


}

Jun 06, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here