变量不能明确

Variables cannot be clear

本文关键字:不能 变量      更新时间:2023-10-16

所以我在运行它并输入正确答案时编写了这些代码,它工作得很好,但是如果我输入错误的答案并使用 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:

如果您使用的是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;    
}

使用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 . . .```
相关文章: