if语句在有两个OR选项的第三个条件下不起作用

if statment not working with 3rd condition having 2 OR options

本文关键字:不起作用 条件下 选项 三个 两个 语句 if OR      更新时间:2023-10-16

我想得到的结果只有当所有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")) {