变量不清晰

时间:2019-10-13 05:36:44

标签: c++

因此,当我运行这些代码并输入正确的答案时,我编写了这些代码,它工作得很好,但是如果我输入了错误的答案并使用while循环再次运行程序,则结果将存储最后的输入并执行不要再让我输入了。

我发现您可以添加guess.clear();来清除存储在变量“ guess”中的内存。

#include <iostream>
#include <string>
#include <inttypes.h>

using namespace std;

int main(){

    int replay = 0;
    int input = 0;

    while(input == replay){
        string celebrity = "Keeves Reeves";
        string guess;
        guess.clear();


        cout << "Hint: He acted as John Wick." << endl;
        cout << "Guess who's this celebrity is: ";
        getline(cin, guess);


        if(guess == celebrity){
            cout << "Congratulations! You got it!\n" << endl;
            input = 2;
        } else if(guess != celebrity){
            cout << "Whoops, that not the right answer..." << endl;
            cout << "Do you want to try again? Enter '0' to replay or enter '1' to view the answer: ";
            cin >> input;
            if (input == 1){
                cout << "The answer is " + celebrity  + "." << endl;
                input = 2;
            }
        }
    }


    cout << "Thank you for playing. The end." << endl;
    system("pause");

    return 0;
}

结果:

Hint: He acted as John Wick.
Guess who's this celebrity is: Michael
Whoops, that not the right answer...
Do you want to try again? Enter '0' to replay or enter '1' to view the answer: 0
Hint: He acted as John Wick.
Guess who's this celebrity is: Whoops, that not the right answer...
Do you want to try again? Enter '0' to replay or enter '1' to view the answer:

2 个答案:

答案 0 :(得分:1)

如果您使用的是getline(),则可以添加cin.ignore()来清除缓冲区,如下所示:

int main(){

    int replay = 0;
    int input = 0;

    while(input == replay){
        string celebrity = "Keeves Reeves";
        string guess;
        //guess.clear();


        cout << "Hint: He acted as John Wick." << endl;
        cout << "Guess who's this celebrity is: ";
        getline(cin, guess);


        if(guess == celebrity){
            cout << "Congratulations! You got it!\n" << endl;
            input = 2;
        } else if(guess != celebrity){
            cout << "Whoops, that not the right answer..." << endl;
            cout << "Do you want to try again? Enter '0' to replay or enter '1' to view the answer: ";
            cin >> input;
            cin.ignore()  // here add the ignore() function.
            if (input == 1){
                cout << "The answer is " + celebrity  + "." << endl;
                input = 2;
            }
        }
    }
    cout << "Thank you for playing. The end." << endl;    
}

答案 1 :(得分:0)

您是否需要使用getline? 通过使用cin >> guess可以正常工作。

代码:

int replay = 0;
int input = 0;

while (input == replay) {
    string celebrity = "Keeves Reeves";
    string guess = "";

    cout << "Hint: He acted as John Wick." << endl;
    cout << "Guess who's this celebrity is: ";
    cin >> guess;


    if (guess == celebrity) {
        cout << "Congratulations! You got it!\n" << endl;
        input = 2;
    }
    else if (guess != celebrity) {
        cout << "Whoops, that not the right answer..." << endl;
        cout << "Do you want to try again? Enter '0' to replay or enter '1' to view the answer: ";
        cin >> input;
        if (input == 1) {
            cout << "The answer is " + celebrity + "." << endl;
            input = 2;
        }
    }
}


cout << "Thank you for playing. The end." << endl;
system("pause");

return 0;

}

输出:

Guess who's this celebrity is: a
Whoops, that not the right answer...
Do you want to try again? Enter '0' to replay or enter '1' to view the answer: 0
Hint: He acted as John Wick.
Guess who's this celebrity is: a
Whoops, that not the right answer...
Do you want to try again? Enter '0' to replay or enter '1' to view the answer: 0
Hint: He acted as John Wick.
Guess who's this celebrity is: a
Whoops, that not the right answer...
Do you want to try again? Enter '0' to replay or enter '1' to view the answer: 1
The answer is Keeves Reeves.
Thank you for playing. The end.
Press any key to continue . . .```