矢量没有清除

时间:2016-01-18 21:41:51

标签: c++

我创建了一个函数,它从用户那里得到一系列猜测(一系列颜色)并将它们放在一个向量中,并在main()中的while循环中调用此函数。

每次while循环调用时,应在重新填充输入之前清除猜测。但是,在第二个循环中,输入我在第一个循环中输入的颜色会激活我的错误消息("无效或重复的颜色输入......"),表明矢量未成功清除。

我试图用空格,各种琴弦等清除它,但似乎没有什么可以清除它。我错过了什么?

功能:

void getGuess(vector<string> &currentGuessPegs, vector<string> &colorChoices, int maxPegSlots) {

string input; // stores input temporarily

// ---clear previous guess---
for (int i = 0; i < maxPegSlots; i++) {
    currentGuessPegs[i] == "";
}

// ---prompt player for each peg guess and store in currentGuessPegs---
for (int i = 0; i < maxPegSlots; i++) {
    cout << "Peg " << i+1 << ": ";
    cin >> input;

    while (find(currentGuessPegs.begin(), currentGuessPegs.end(), input) != currentGuessPegs.end() // Loops if color entry has already been used
        || find(colorChoices.begin(), colorChoices.end(), input) == colorChoices.end()) {         //  or is an invalid choice

        cout << "Invalid or repeated color entry. See color choices and re-enter a color you have not used.\n";
        cout << "Peg " << i + 1 << ": ";
        cin >> input;
    }

    currentGuessPegs[i] = input;
}
}

这是我对main()函数的调用:

// ---get and check guesses until maximum # of guesses is exceeded or solution is guessed---
while (guessCount < maximumGuesses && solutionGuessed == false) {
    getGuess(currentGuess, colorOptions, numberOfPegs); // get the guess
    solutionGuessed = checkGuess(currentGuess, solution, numberOfPegs, red, white); // check the guess; returns true if solution was guessed

    cout << "r: " << red << "   w: " << white << endl << endl;

    guessCount++;
}

1 个答案:

答案 0 :(得分:4)

currentGuessPegs[i] == "";
//                  ^^

糟糕。