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;
}
You need to call the functions
vowelCount()andremainingLetters()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_letterswill not output anythingMoreover, the function
vowelCount()defines the local variablevowel_countthat hides the one in the class (member variable), so the member variable will not get updated (uless you explicitly assign the return value ofvowelCount()to that member variable) I suggest you delete the local variable declaration from thevowelCount()function and it will use the member variable, which is what you needThe previous paragraph also applies to the member variable
remaining_lettersOne more thing: the memory allocated with
newis 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 eachnew[]call with its correspondingdelete[]call. Better yet: allocate the memory in the constructor of the class and delete it in the destructor for the member variableNote: yet another bug is in this logic:
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):