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

Help with Handman C++ game? I have some written down but I am having trouble wit

ID: 3776757 • Letter: H

Question

Help with Handman C++ game?

I have some written down but I am having trouble with two things

1.) Listing the already used letters

2.) Displaying, "The category is.." when some number is entered asking for a hint.

This is my code so far, Please and thank you,

(it probably needs a little cleaning up)

#include
#include
#include
#include
#include
#include

using namespace std;

const int MaxGuesses = 8;
int letterFill(char, string, string&);


int main()
{
   string unknown, word;
   int num_wrong_guesses = 0,i;
char letter;

   cout << "Are you ready to Play Hangman?!" << endl;

   int category;
   cout << "Please, Choose a Category( 1, 2, or 3): " << endl;
   cin >> category;

   if (category == 1)
   {
       string one[] = { "pineapple" };

       int num = rand() % 1;
       word = one[num];

       string unknown(word.length(), '-');
       cout << "Rules:" << endl;
       cout << "Type one letter at a time." << endl;
       cout << "You have " << MaxGuesses << " attempts at guessing the right letters or word." << endl;

       while (num_wrong_guesses < MaxGuesses)
       {
           cout << " " << unknown << endl;
           cout << " Guess a letter:" << endl;
           cin >> letter;

           if (letterFill(letter, word, unknown) == 0)
           {
               cout << endl << "Incorrect letter! :( Try again." << endl;
               num_wrong_guesses++;
           }

           else

           {
               cout << endl << "Correct letter! :) " << endl;
           }

           cout << "You have " << MaxGuesses - num_wrong_guesses;
           cout << " tries left." << endl;
//for (i=0; i<25; i++)
//cout << letter[i] << endl;

           if (word == unknown)
           {
               cout << word << endl;
               cout << "You Win!";
               break;
           }

       }
       if (num_wrong_guesses == MaxGuesses)
       {
           cout << "Sorry, you lost the game!" << endl;
           cout << "The word was : " << word << endl;
       }
       cin.ignore();
       cin.get();
       return 0;
   }


   else if (category == 2)
   {

       string two[] = { "canada"};

       int num = rand() % 1;
       word = two[num];


       string unknown(word.length(), '-');
cout << "Rules:" << endl;
       cout << "Type one letter at a time." << endl;
       cout << "You have " << MaxGuesses << " attempts at guessing the right letters or word." << endl;


       while (num_wrong_guesses < MaxGuesses)
       {
           cout << " " << unknown;
           cout << " Guess a letter: ";
           cin >> letter;

           if (letterFill(letter, word, unknown) == 0)
           {
               cout << endl << "Incorrect letter! :( Try again " << endl;
               num_wrong_guesses++;
               system("cls");
           }
           else
           {
               cout << endl << "Correct Letter! :) " << endl;
           }

           cout << "You have " << MaxGuesses - num_wrong_guesses;
           cout << " tries left." << endl;

           if (word == unknown)
           {
               cout << word << endl;
               cout << "You Win!";
               break;
           }
       }
       if (num_wrong_guesses == MaxGuesses)
       {
           cout << "Sorry, you lost the game." << endl;
           cout << "The word was : " << word << endl;
       }
       cin.ignore();
       cin.get();
       return 0;
   }

   else if (category == 3)
   {
       string three[] = { "game_of_thrones" };
       string word;

       int num = rand() % 1;
       word = three[num];

       string unknown(word.length(), '-');
       cout << "Rules:" << endl;
       cout << "Type one letter at a time." << endl;
       cout << "You have " << MaxGuesses << " attempts at guessing the right letters or word." << endl;
       cout << "This word contains a space, Please use an underscore, '_' for spaces. " << endl;

       while (num_wrong_guesses < MaxGuesses)
       {
           cout << " " << unknown;
           cout << " Guess a letter:";
           cin >> letter;

           if (letterFill(letter, word, unknown) == 0)
           {
               cout << endl << "Incorrect letter! :( Try again." << endl;
               num_wrong_guesses++;
               system("cls");
           }
           else
           {
               cout << endl << "Correct letter! :) " << endl;

           }

           cout << "You have " << MaxGuesses - num_wrong_guesses;
           cout << " guesses left." << endl;

           if (word == unknown)
           {
               cout << word << endl;
               cout << "You Win!";
               break;
           }
       }
       if (num_wrong_guesses == MaxGuesses)
       {
           cout << "Sorry, you lost the game!" << endl;
           cout << "The word was : " << word << endl;
       }
       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; i++)
   {

       if (guess == guessword[i])
           return 0;

       if (guess == secretword[i])
       {
           guessword[i] = guess;
           matches++;
       }
   }
   return matches;
}

Explanation / Answer

#include<iostream>
#include<string>
using namespace std;
const int MaxGuesses = 8;
int letterFill(char, string, string&);

int main()
{
string unknown, word;
int num_wrong_guesses = 0;
char letter;
cout << "Are you ready to Play Hangman?!" << endl;
int category;
cout << "Please, Choose a Category( 1, 2, or 3): " << endl;
cin >> category;
if (category == 1)
{
string one[] = { "pineapple" };
int num = rand() % 1;
word = one[num];
string unknown(word.length(), '-');
cout << "Rules:" << endl;
cout << "Type one letter at a time." << endl;
cout << "You have " << MaxGuesses << " attempts at guessing the right letters or word." << endl;
while (num_wrong_guesses < MaxGuesses)
{
cout << " " << unknown << endl;
cout << " Guess a letter:" << endl;
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "Incorrect letter! :( Try again." << endl;
num_wrong_guesses++;
}
else
{
cout << endl << "Correct letter! :) " << endl;
}
cout << "You have " << MaxGuesses - num_wrong_guesses;
cout << " tries left." << endl;
//for (i=0; i<25; i++)
//cout << letter[i] << endl;
if (word == unknown)
{
cout << word << endl;
cout << "You Win!";
break;
}
}
if (num_wrong_guesses == MaxGuesses)
{
cout << "Sorry, you lost the game!" << endl;
cout << "The word was : " << word << endl;
}
cin.ignore();
cin.get();
return 0;
}

else if (category == 2)
{
string two[] = { "canada"};
int num = rand() % 1;
word = two[num];

string unknown(word.length(), '-');
cout << "Rules:" << endl;
cout << "Type one letter at a time." << endl;
cout << "You have " << MaxGuesses << " attempts at guessing the right letters or word." << endl;

while (num_wrong_guesses < MaxGuesses)
{
cout << " " << unknown;
cout << " Guess a letter: ";
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "Incorrect letter! :( Try again " << endl;
num_wrong_guesses++;
system("cls");
}
else
{
cout << endl << "Correct Letter! :) " << endl;
}
cout << "You have " << MaxGuesses - num_wrong_guesses;
cout << " tries left." << endl;
if (word == unknown)
{
cout << word << endl;
cout << "You Win!";
break;
}
}
if (num_wrong_guesses == MaxGuesses)
{
cout << "Sorry, you lost the game." << endl;
cout << "The word was : " << word << endl;
}
cin.ignore();
cin.get();
return 0;
}
else if (category == 3)
{
string three[] = { "game_of_thrones" };
string word;
int num = rand() % 1;
word = three[num];
string unknown(word.length(), '-');
cout << "Rules:" << endl;
cout << "Type one letter at a time." << endl;
cout << "You have " << MaxGuesses << " attempts at guessing the right letters or word." << endl;
cout << "This word contains a space, Please use an underscore, '_' for spaces. " << endl;
while (num_wrong_guesses < MaxGuesses)
{
cout << " " << unknown;
cout << " Guess a letter:";
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "Incorrect letter! :( Try again." << endl;
num_wrong_guesses++;
system("cls");
}
else
{
cout << endl << "Correct letter! :) " << endl;
}
cout << "You have " << MaxGuesses - num_wrong_guesses;
cout << " guesses left." << endl;
if (word == unknown)
{
cout << word << endl;
cout << "You Win!";
break;
}
}
if (num_wrong_guesses == MaxGuesses)
{
cout << "Sorry, you lost the game!" << endl;
cout << "The word was : " << word << endl;
}
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; i++)
{
if (guess == guessword[i])
return 0;
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