我正在 Code::Blocks (显然是 C++ ;))中创建一个基于文本的刽子手游戏。
所以我创建了一个数组 char knownLetters[]; 但我不知道这个词会有多长时间,我如何计算字符串中有多少个字符?
代码:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
string GenerateWord() // Generate random word to be used.
{
srand(time(NULL));
int wordID = rand() % 21;
string wordList[20] = {"Dog" ,"Cat","Lion","Ant","Cheetah","Alpaca","Dinosaur","Anteater","Shark","Fish","Worm","Lizard","Bee","Bird","Giraffe","Deer","Crocodile","Wife","Alligator","Yeti"};
string word = wordList[wordID];
return word;
}
void PrintLetters() // Display the word including underscores
{
string word = "";
char knownLetters[word];
for(int pos = 0; pos < word.length(); pos++) {
if(knownLetters[pos] == word[pos]) cout << word[pos];
else cout << "_";
}
cout << "\n";
}
void PrintMan() // Display the Hangman to the User
{
// To Be completed
}
void PlayerLose() // Check For Player Loss
{
// To Be completed
}
void PlayerWin() // Check For Player Win
{
// To Be completed
}
int main()
{
cout << "Hello world!" << endl;
return 0;
}
提前致谢!