我的布尔函数有问题。当我编译程序时,一切运行得很好,但是当我输入" 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);
}
答案 0 :(得分:3)
Your logic is backwards - verification
returns false
for anything that isn't "yes"
. Since "no"
isn't "yes"
, verification("no")
returns false
, and in the main
function you print out this message if !verification("no")
, which evaluates to true
.
Seems like you should drop the !
operator from the if
statement.
答案 1 :(得分:1)
键入yes后会发生什么? 发生什么事情是当你输入no时,它返回false。然后你将其反转(!)为真。它运行正常,但你正在翻转它,所以它不仅仅是“是”,它实际上适用于“是”的所有内容。
删除! (不是操作员),它将按预期工作。