当然,我是初学者。我试图创建一个简单的短语猜测游戏。所以我随机生成了一个短语。我想设置一个等于随机短语长度的变量,这将改变每个游戏。随着游戏的进行和正确的猜测,我想用正确的猜测来填写下面的分数。
t _ e(此处为空格)p _ r a _ e
这种事情。现在我只是试图用空格和下划线生成揭示短语。提前谢谢。
string phrase = verbs[random_word1] + ' ' + nouns[random_word2] + ' ' + conjunct[random_conjunct] + ' ' + result + ' ' + adjectives[random_word3];
string revealPhrase = "";
for (i=0; i < phrase.length(); i++){
if (phrase.at[i] == ' ')
revealPhrase = ' ';
else
revealPhrase = revealPhrase + "_ ";}
cout << revealPhrase << endl;
答案 0 :(得分:0)
也许
for (int i=0; i < phrase.length(); i++){
if (phrase[i] == '-')
revealPhrase += " "; // assuming you want spaces here
else
revealPhrase += "_ ";
}
答案 1 :(得分:0)
尝试这样的事情
char c;
string actualPhrase = "the phrase";
string revealPhrase = "__________";
int length = actualPhrase.length();
int curIndex;
while(actualPhrase.compare(revealPhrase) != 0)
{
cin >> c;
for(int i=0; i < length; i += 1)
{
if(actualPhrase[i] == c)
{
revealPhrase[i] = c;
}
}
}