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

Working on the C++ program. I finish most of part, but output is wrong, so can s

ID: 3565824 • Letter: W

Question

Working on the C++ program. I finish most of part, but output is wrong, so can someone show me what is wrong with my code?

// hangMan.cpp
// "Hangman" word guessing game.

#include
#include
using namespace std;

// Function prototypes
void createTemplate(const char secretWord[], char guessTemplate[]);

void updateTemplate(const char secretWord[], char guessLetter,
   char guessTemplate[]);

bool matchTemplate(const char secretWord[], const char guessTemplate[]);

//--------------------------------------------------------------------

int main()
{
   const int NUM_LINES = 25; // Number of lines on the screen
   const int MAX_SIZE = 15;

   char secretWord[MAX_SIZE], // Secret word to be guessed
       guessTemplate[MAX_SIZE]; // Template showing correct guesses
   char guessLetter; // Letter guessed

   int numGuesses, // Number of letters guessed
       j; // Counter

   // Get the secret word.
   cout << endl << "Enter the secret word: ";
   cin >> setw(MAX_SIZE) >> secretWord;

   // Scroll it off the screen.
   for (j = 0; j < NUM_LINES; j++)
       cout << endl;

   // Create an empty guess template.
   createTemplate(secretWord, guessTemplate);

   // Play the game.
   numGuesses = 0;
   while (!matchTemplate(secretWord, guessTemplate))
   {
       numGuesses++;
       cout << guessTemplate << endl;

       // Get a letter guess.
       cout << "Guess a letter: ";
       cin >> guessLetter;

       // Fill in the occurrences of this letter.
       updateTemplate(secretWord, guessLetter, guessTemplate);
   }

   // Display the secret word and the number of guesses.
   cout << guessTemplate << "=" << secretWord << endl;
   cout << "You guessed the word in " << numGuesses << " guesses"
       << endl;

   return 0;
}


//--------------------------------------------------------------------
// Insert your function implementations here.
//--------------------------------------------------------------------
void createTemplate(const char secretWord[], char guessTemplate[])
{
   // The guessTemplate will be an array of characters, all cells
   // filled with the char '-' and with a '' at the end
   // The number of cells filled will be determined by the numbers
   // of characters in secretWord
   // If secretWord is the C++ string monday
   // Then guessTemplate will be ------

   // ONLY ONE LOOP IS ALLOWED IN THIS FUNCTION

   int i = 0, n = 0;
   while (secretWord[i] != '')
   {
       i++;
   }
   for (n; n < i; n++)
   {
       guessTemplate[n] = '-';
   }
}


void updateTemplate(const char secretWord[], char guessLetter,
   char guessTemplate[])
{
   // Update guessTemplate by replacing '-' in the array with
   // guessLetter for each corresponding cell in secretWord
   // If secretWord is kansas and guessLetter is 's'.
   // If guessTemplate was --n--- then guessTemplate would
   // be changed to --ns-s


   // ONLY ONE LOOP IS ALLOWED IN THIS FUNCTION

   int i = 0;
   while (secretWord[i] != '')
   {
       if (secretWord[i] == guessLetter)
           guessTemplate[i] = guessLetter;
       i++;
   }


}


bool matchTemplate(const char secretWord[], const char guessTemplate[])
{

   // will return true if secretWord and guessTemplate contain the same
   // C++ string and will return false otherwise

   // ONLY ONE LOOP IS ALLOWED IN THIS FUNCTION

   int i = 0;
   while (secretWord[i] != '')
   {
       if (secretWord[i] == guessTemplate[i])
           return true;
       else
           return false;
   }
  
}

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

const int MAX_TRIES=5;

int letterFill (char, string, string&);

int main ()
{
string name;
char letter;
int num_of_wrong_guesses=0;
string word;
string words[] =
{
"india",
"pakistan",
"nepal",
"malaysia",
"philippines",
"australia",
"iran",
"ethiopia",
"oman",
"indonesia"
};

//choose and copy a word from array of words randomly
srand(time(NULL));
int n=rand()% 10;
word=words[n];

// Initialize the secret word with the * character.
string unknown(word.length(),'*');

// welcome the user
cout << " Welcome to hangman...Guess a country Name";
cout << " Each letter is represented by a star.";
cout << " You have to type only one letter in one try";
cout << " You have " << MAX_TRIES << " tries to try and guess the word.";
cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Loop until the guesses are used up
while (num_of_wrong_guesses < MAX_TRIES)
{
cout << " " << unknown;
cout << " Guess 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!" << endl;
num_of_wrong_guesses++;
}
else
{
cout << endl << "You found a letter! Isn't that exciting!" << endl;
}
// Tell user how many guesses has left.
cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
cout << " guesses left." << endl;
// Check if user guessed the word.
if (word==unknown)
{
cout << word << endl;
cout << "Yeah! You got it!";
break;
}

}
if(num_of_wrong_guesses == MAX_TRIES)
{
cout << " Sorry, you lose...you've been hanged." << endl;
cout << "The word was : " << word << endl;
}
cin.ignore();
cin.get();
return 0;
}

/* Take a one character guess and the secret word, and fill in the
unfinished guessword. Returns number of characters matched.
Also, returns zero if the character is already guessed. */

int letterFill (char guess, string secretword, string &guessword)
{
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++)
{
// 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;
}

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