gpt4 book ai didi

c++ - boolean 函数问题

转载 作者:行者123 更新时间:2023-11-30 01:47:33 25 4
gpt4 key购买 nike

我的 boolean 函数有问题。当我编译程序时一切正常,但是当我输入“否”时它仍然说“我能为您提供什么帮助?”。

#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 对任何不是 "yes" 的内容返回 false。由于 "no" 不是 "yes"verification("no") 返回 false,并且在如果 !verification("no") 计算结果为 true,则 main 函数打印出此消息。

看来您应该从 if 语句中删除 ! 运算符。

关于c++ - boolean 函数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31487631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com