布尔函数问题
Boolean function problems
布尔函数有问题。当我编译程序时,一切运行正常,但是当我输入"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。它工作得很好,但是你翻转了它,所以它不是只工作在"是"上,它实际上可以工作在除"是"之外的所有地方。
删除!
相关文章:
- Visual Studio中的函数声明和函数定义问题
- C++quit()函数中可能存在作用域问题
- 类似于strcat()的函数出现问题
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 类C++中的函数问题(LNK2019和LNK1120错误)
- c++binary_search函数排序数组(流行名称搜索)出现问题
- 关于简单C++函数(is_palindrome)的逻辑的问题
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- constexpr构造函数需要常量成员函数时出现问题
- 添加存储在向量中的大整数的函数出现问题
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 关于 c++ 函数中指针赋值的简单问题
- 如何在标头中声明(或定义)函数的问题
- 有关 c++ 构造函数的问题
- 模板类转换问题 - 无法推断调用的函数
- 从 C++ 中的函数返回数组地址问题
- 关于复制构造函数的一个棘手问题
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题