我编写了此代码以使用if-else语句,但“no”输出与yes相同,我首先尝试在本地声明yes和no变量,这解决了我得到的第一个错误。但是现在他们无法区分产出。无论输入是什么,是和不输出的条件。我的if-else语句中的no选项无法正常工作
这里是下面的代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name;
bool answer;
cout<<"Welcome user 'Divine 9'..."<<"What is your name?"<<endl;
getline(cin, name);
cout<<endl<<"Hello "<<name<<", my name is Xavier."<<endl<<" I am going to ask you some questions about yourself. Fear not, i will not take any of your information back to the boss man, or store it."<<endl;
cout<<"Is this okay with you? (yes/no)"<<endl;
cin>>answer;
{
bool yes;
bool no;
if(answer==yes)
cout<<"Great, will proceed with the questions!"<<endl;
else (answer==no)
cout<<"That is okay.";
}
return 0;
}
所以,如果我输入yes,它会输出:
“伟大的,将与问题继续进行。”
没关系。
如果我输入no,它会输出相同。 有人可以帮我解决这个问题吗?我以为我有它,但我想我不会
如果您对此代码没有*至少*两个警告,请配置您的编译器以更好地帮助您。 – nvoigt 2014-12-27 17:05:29
这应该不会编译,除非在else(answer == no)和'cout'之间还有一个额外的';'。 – 2014-12-27 17:06:07