我正在写一个hang子手游戏。我的逻辑失败,无论是我自己还是我的游戏逻辑。字符猜测(人们猜测的字母)没有被添加到矢量guessArray的正确内存插槽中。假设该词是用户输入的词。std :: vector与C++中的原始数组有多类似?
我猜这会工作,如果guessArray是一个原始数组。有没有一些原因,这不是与矢量工作?
//assume that attempts is num of attempts left
void coutWord(int attempts, std::string word, char guess)
{
std::vector<char> guessArray(word.length());
//this is supposed to add the guess to the correct area of guessArray;
//It's not.
for (int i = 0; i < word.length(); i++) {
if (guess == word[i]) {
guessArray[i] = guess;
std::cout << " " << guessArray[i] << " ";
continue;
}
std::cout << " _ ";
}
std::cout << std::endl << std::endl;
}
编辑:我使用此代码的目的是来清点所有unguessed空间,在同一个for循环的猜测空间。我只需要“记住”以前的猜测,以便我得到正确的输出。定的字=“苹果酱”:
Input: a
a _ _ _ _ _ a _ _ _
Input: p
a p p _ _ _ a _ _ _
等
这似乎是相当bug的代码,不管是否guessArray是一个载体或原始阵列。 为了能够回答你的问题,你需要说出你在这里尝试做什么。 EG,如果单词是“Triangulate”,并且猜测是“g”,那么输出应该是什么? – 2009-04-27 03:32:07
那么我应该通过矢量作为另一个参数? – jkeys 2009-04-27 03:50:44