我的 bool(boolean) 函数遇到问题。当我编译程序时,一切运行正常,但是当我输入“no”时,它仍然显示“我能为您提供什么帮助?”。
#include <iostream>
#include <string> //size()
#include <cctype> //isdigit()
using namespace std; //(xxx)xxx-xxxx
bool verification(string yesOrno)
{
if(yesOrno == "yes")return(true);
else return(false);
}
int main()
{
string yesOrno;
cout <<"Do you need more help\n";
cin >> yesOrno;
if(!verification(yesOrno))cout <<"What can I help you with?\n";
return(0);
}
最佳答案
您的逻辑是倒退的-verification
对于不是false
的任何内容返回"yes"
。由于"no"
不是"yes"
,因此verification("no")
返回false
,并且在main
函数中,如果输出!verification("no")
,则打印出此消息,其结果为true
。
似乎您应该从!
语句中删除if
运算符。