变量不能明确
Variables cannot be clear
所以我在运行它并输入正确答案时编写了这些代码,它工作得很好,但是如果我输入错误的答案并使用 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 . . .```
相关文章:
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- 为什么我不能在返回 const 的布尔函数中为类成员变量赋值?C++
- 为什么我不能像这样在静态成员函数中调用静态成员变量?
- 为什么我们不能在C++中初始化类的成员变量
- 为什么我不能为变量赋值函数?
- 我们不能在 c++ 中只有对象的变量吗?
- 在C++中,为什么int可以使用new运算符初始化变量,而double不能
- 表达式必须具有常数值,变量不能用作定义数组大小的常数
- 返回实例变量的c++方法可以访问变量中的数据,但不能更改它,但在编译时不会生成错误
- 为什么我不能使用最小和最大这两个词作为变量名称?
- 变量不能明确
- 如果静态变量只为程序的整个部分存储了一个副本,为什么我不能使用静态变量交换 2 个数字?
- 流变量不能出现在 OpenMP 第一私有中?
- 为什么 UInt64 变量不能包含大于 UInt32::Max 的值?
- 变量不能在 lambda 中隐式捕获,并且没有使用 switch 语句指定捕获默认值
- 为什么我不能在类中设置静态变量的值?
- 流的奇怪问题.不能存储具有特定值的成员变量的对象