if语句在有两个OR选项的第三个条件下不起作用
if statment not working with 3rd condition having 2 OR options
我想得到的结果只有当所有3个条件都满足,代码工作良好,当第3只有一个选项前"史密斯",但只采取1个条件,如果第3有2个或更多的前"史密斯"||"史密斯"
#include <iostream>
using namespace std;
int main()
{
int age;
string name;
string lastname;
cout << "Podaj swoje imie" << endl;
cin >> name;
cout << "Podaj swoje nazwisko" << endl;
cin >> lastname;
cout << "Wprowadz swoj wiek" << endl;
cin >> age;
cout << "Imie: ";
cout << name << endl;
cout << "Nazwisko: ";
cout << lastname << endl;
cout << "Wiek: ";
cout << age << endl;
if (age < 18)
{
cout << "you're too young";
}
else
{
cout << "you can drink" << endl;
}
if ((age == 27)
&& (name == "katarzyna" || "kasia" || "Katarzyna" || "Kasia")
&& (lastname == "smith" || "Smith"))
{
cout << "BOOM" << endl;
cout << "BOOM";
}
}
lastname == "smith" || "Smith"
并不像你想象的那样。
如果lastname == "smith"
为真,或者"Smith"
为真,"Smith"
总是计算为一个非null指针,所以它总是为真。
您需要lastname == "smith" || lastname == "Smith"
,第二部分同样需要name
。
我将这样做:
if ((age == 27) && (name == "katarzyna" || name == "kasia" || name == "Katarzyna" || name == "Kasia") && (lastname == "smith" || lastname == "Smith")) {
相关文章:
- 为什么在保护模式下继承升级不起作用
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么 ulimit -v 在 Clang 的地址清理器下不起作用?
- G++ C++17 类模板参数推导在非常特殊的情况下不起作用
- 条件不起作用,谁能解释为什么会这样?
- EOF有更安全的替代方案吗?它在我的情况下不起作用
- 在函数中声明的 ifStream 对象在发布模式下不起作用
- 在 directx11 代码中更改全屏模式下的刷新率不起作用
- c++ 选择排序在特定情况下不起作用
- 为什么在这种情况下,前向声明不起作用?
- 寻找有关为什么此C++代码在没有引用的情况下不起作用的解释
- 将指针传递给函数在简单的情况下有效,但在"class" -izing 之后不起作用
- 在某些情况下,多个 OpenGL 纹理不起作用?
- Makefile在Linux中工作,但在Windows下不起作用,在子目录中找不到文件
- 句子转换器在存在新行的情况下不起作用
- 为什么 std::void_t 在这种情况下不起作用
- 为什么这个条件变量不起作用
- 我的代码在发布模式下不起作用,而在Qt的调试模式下工作
- if语句在有两个OR选项的第三个条件下不起作用
- 为什么逻辑运算符 false 在条件下不起作用