我正在开发一个刽子手程序,我想在每次猜测后向用户输出状态报告。当我使用类时,我需要使用“朋友”关键字。我对班级和朋友的概念很满意,但是我正在努力在我的程序中正确实现它。
主要问题是没有计算元音的数量,并且用户可以尝试的字母数量没有更新。每次都显示完整的字母表。
我的程序代码很丰富,我不打算在这里全部发布。我遇到的问题是功能remainingLetters()
;和vowelCount()
。我附上了相关的代码片段。这段代码显然不会编译。我只是希望有人可能会看到我错过的明显错误。
**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;
}