Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7768171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:51:09+00:00 2026-06-01T15:51:09+00:00

I am working on a hangman program and I want to output a status

  • 0

I am working on a hangman program and I want to output a status report to the user after each guess. As I am using classes, I need to use a ‘friend’ keyword. I’m ok with the concept of classes and friends however I am struggling to implement it properly in my program.

The main issue is that the numbers of vowels is not being counted and the number of letters the user can try is not updating. The full alphabet is being displayed each time.

The code of my program is substantial and I’m not going to post all of it here. The issues I have are with the functions remainingLetters(); and vowelCount(). I have attached the relevant code snippets. This code will obviously not compile. I’m just hoping that someone might see an obvious mistake that I’ve missed.

**Hangman.h:**
{
public:
...
int vowelCount()const; //to be implemented
char* remainingLetters()const;//to be implemented
friend ostream& operator<< (ostream &out, Hangman &game);

private:
...
int vowel_count;
char secretWord[MAXWORDLENGTH];
char* remaining_letters;
}

**hangman.cpp:**
{
...
int Hangman::vowelCount()const
{
  int vowel_count = 0;
  int i;
  secretWord[i];

  for(i=0; i<strlen(secretWord); i++)
  {


if(secretWord[i]=='a'||secretWord[i]=='e'||secretWord[i]=='i'||secretWord[i]=='o'||secretWord[i]=='u')
    {
      vowel_count++;
    }
  }
  return vowel_count;
}

char* Hangman::remainingLetters()const
{
  char alphabet[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  char *remaining_letters=new char[ALPHABETSIZE];

  for(int i=0; i<strlen(secretWord); i++)
  {
  for(int j=0; j<ALPHABETSIZE; j++)
    {
       if(secretWord[i]!=alphabet[j])
       {
     remaining_letters[j]=alphabet[j];
       }
    }
  } 
 return remaining_letters;
}

ostream& operator<< (ostream &out, Hangman &game)
{
    out << "Number of guesses remaining is: " << game.numGuessesAllowed-game.numWrongGuesses << endl
     << "Number of incorrect guesses so far is: "<<game.numWrongGuesses <<endl
     << "Letters remaining are: "<<game.remaining_letters <<endl
     << "Hint: The secret word contains "<<game.vowel_count <<" vowels"<<endl;

    return out;
}
}

**main.cpp**
{
...
 cout << game;
...
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:51:10+00:00Added an answer on June 1, 2026 at 3:51 pm

    You need to call the functions vowelCount() and remainingLetters() for the corresponding variables to be calculated (so that you can output them).

    Also, char *remaining_letters=new char[ALPHABETSIZE]; will allocate the memory for the letters and initialize each position with 0 — which also happens to be the string terminating value. So if the first position (letter 'a') is not set, out<<game.remaining_letters will not output anything

    Moreover, the function vowelCount() defines the local variable vowel_count that hides the one in the class (member variable), so the member variable will not get updated (uless you explicitly assign the return value of vowelCount() to that member variable) I suggest you delete the local variable declaration from the vowelCount() function and it will use the member variable, which is what you need

    The previous paragraph also applies to the member variable remaining_letters

    One more thing: the memory allocated with new is not deallocated automatically, and once you return from the function call, the allocated memory is lost (as in there is no way to access it again, but it still uses memory), unless you assign it when the function returns. You will need to pari each new[] call with its corresponding delete[] call. Better yet: allocate the memory in the constructor of the class and delete it in the destructor for the member variable

    Note: yet another bug is in this logic:

    for(int i=0; i<strlen(secretWord); i++) 
    { 
      for(int j=0; j<ALPHABETSIZE; j++) 
      { 
        if(secretWord[i]!=alphabet[j]) 
        { 
          remaining_letters[j]=alphabet[j]; 
        } 
      } 
    }
    

    you cycle through the alphabet and every time the current letter in secret word is not matvching, you assign to remaining_letters. This will assignment will happen every time as there will be a letter in the alphabet that does not match the curent secretWord letter.

    To fix this, you will need do something like this (note the inversion of the loops):

    int secret_len = strlen(secretWord); // cache length, so we don't recalculate it
                                         // every time: secretWord does not change
    for(int j=0; j<ALPHABETSIZE; j++) 
    { 
      bool found = false; // assume the current ABC letter is not in secretWord
      for(int i=0; i<secret_len; i++) 
      { 
        if(secretWord[i]!=alphabet[j]) 
        { 
          found = true; // but it was
        } 
      } 
      if (!found) // if it wasn't
      {
        remaining_letters[j]=alphabet[j]; 
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a program but can't understand working with pointers when classes
I'm working on a Python version of hangman that works off of a .txt
Working on a website that has Employee and Branch entities, using a database table
Working on a rather small, and simple layout, I decided to use Meyer's CSS
Working on an extension that use the new experimental devtools apis. How do you
Working on an old Kohana 2 project and I want to link two models.
Working with an API where I need to send a value over in an
Working sample using one Table SELECT t.* FROM ( SELECT TITLE.name, (TITLE.value-TITLE.msp) AS Lower,
Working in Java: I have a JFrame class, and separate classes for my two
Working in Eclipse on a Dynamic Web Project (using Tomcat (v5.5) as the app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.