布尔函数问题

Boolean function problems

本文关键字:问题 函数 布尔      更新时间:2023-10-16

布尔函数有问题。当我编译程序时,一切运行正常,但是当我输入"no"时,它仍然显示"what can I help you with?"

#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 helpn";
    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语句中删除!操作符。

当你输入yes时会发生什么?当你输入no时,它会返回false。然后将(!)反转为true。它工作得很好,但是你翻转了它,所以它不是只工作在"是"上,它实际上可以工作在除"是"之外的所有地方。

删除!