#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
int main ()
{
cout << "My Funny Program";
do {
string Answer;
cout << "Are you stupid? (Yes/No)";
cin >> Answer;
cout << endl;
cout << "Perhaps you are lying?";
}
while (Answer == "No");
cout << "Thank you for your honesty." << endl;
return 0;
}
我一直在尝试让这段代码正常工作;但是,每次出现“在此范围内未声明答案”错误时,都会弹出。
答案 0 :(得分:1)
我认为如果块本身,while
条件超出了范围。所以string Answer
必须在循环之外定义。
样式建议:Caps用于类或常量或宏。避免将它们用于变量。
答案 1 :(得分:0)
do{
string Answer;
cout<< "Are you stupid? (Yes/No)";
cin>>Answer;
cout<< endl;
cout<< "Perhaps you are lying?";
}
while (Answer == "No");
由于在do-while内部声明了答案,while部分不知道答案是否存在。
您必须在do-while
之前声明答案答案 2 :(得分:0)
您的Answer
变量目前仅在声明它的{}
之间(在循环体内)。您需要在循环之前声明Answer
。
string Answer;
do {
cout << "Are you stupid? (Yes/No)";
cin >> Answer;
cout << endl;
cout << "Perhaps you are lying?";
}
while (Answer == "No");
答案 3 :(得分:0)
有趣的节目......
在do-while循环之外声明答案将解决您的问题。
string Answer;
do {
cout << "Are you stupid? (Yes/No)";
cin >> Answer;
cout << endl;
cout << "Perhaps you are lying?";
}
while (Answer == "No");
此外,您应该使用<string>
代替<string.h>
答案 4 :(得分:0)
do {
string Answer;
cout << "Are you stupid? (Yes/No)";
cin >> Answer;
cout << endl;
cout << "Perhaps you are lying?";
}
while (Answer == "No");
在do
中,您声明了一个字符串变量Answer
。然后Answer
被推入堆栈,但是,当程序强制}
时,变量将被加载,这意味着,在while
语句中,Answer
已经释放,编译器将Answer
视为未定义的变量。