【发布时间】:2014-12-27 16:57:33
【问题描述】:
我编写此代码以使用 if-else 语句,但“no”输出与 yes 相同,我首先尝试在本地声明 yes 和 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,它会输出相同的结果。 有人可以帮我解决这个问题吗?我以为我有,但我想我没有
【问题讨论】:
-
如果此代码没有至少两次警告,请配置您的编译器以更好地帮助您。
-
那甚至不应该编译,除非你在
else (answer == no)和cout之间还有一个额外的;。
标签: c++ if-statement output