查找字符数组长度 [英] Find length of char array
本文介绍了查找字符数组长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创造了code :: Blocks的(C ++很明显;)一个基于文本的刽子手游戏)。
I'm creating a text-based hangman game in Code::Blocks ( C++ Obviously ;) ).
所以,我创建了一个数组的char knownLetters [];但我不知道这个词有多长,我怎么能计算出有多少字符的出现将会在字符串中?
So i've created an array char knownLetters[]; but i'll have no idea how long the word will be, how can i calculate how many char's there will be in the string?
code:
#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;
}
先谢谢了!
推荐答案
从随机字符串生成您使用尺寸
方法找到它的大小
From the random string generated you use the size
method to find its size
http://www.cplusplus.com/reference/string/string/size/
这可以被用于数组的大小或更好的使用载体,那么你并不需要担心的大小
This can then be used for the size of the array or better yet use a vector and then you do not need to worry about the size
void PrintLetters(const std::string& word) // Pass the word in here
{
const int size = word.size();
这篇关于查找字符数组长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文