我似乎在使用基本的 if/else 语句时遇到问题:/

-3 c++ output

想法(C++):

这个想法很简单,如果你未满 21 岁并且接受全日制教育,你就有资格(不知道是什么,这只是家庭作业)。如果您不符合条件,则必须告诉用户原因。

int main()
{
    string education;
    int age;

    cout << "Are you in full time education? (y/n): ";
    cin >> education;

    cout << "\nEnter your age: ";
    cin >> age;
    system("cls");

    if (((education == "yes" || education == "y")) && (age <= 21))
    {
        cout << "You are eligible.";
    }
    else if (((education == "yes" || "y")) && (age > 21))
    {
        cout << "You are not eligible because you are over 21.";
    }
    else if (((education == "no" || "n")) && (age <= 21))
    {
        cout << "You are not eligible because you are not in full time education.";
    }
    else if (((education == "no" || "n")) && (age > 21))
    {
        cout << "You are not eligible because you are not in full time education and you are over 21.";
    }
    else
    {
        cout << "There is a problem with your input.";
    }
}
Run Code Online (Sandbox Code Playgroud)

问题:

现在,如果我输入我没有接受全日制教育并且超过 21 岁,则输出是“你不符合资格,因为你超过 21 岁。”,这在技术上是正确的,但它应该给我“你不符合资格,因为你没有接受全日制教育,而且已经超过 21 岁。” 反而!

注意事项:

  • 我的#include 语句已从屏幕截图中删除,但不要担心它们,我知道它们很好。
  • 所有的“else if”语句最初只是“if”,但我通过这种方式尝试解决问题……但显然无济于事。

Alb*_*lia 5

您不能像这样使用 or 运算符

a == 'first' || 'second' // education == 'yes' || 'y'
Run Code Online (Sandbox Code Playgroud)

为了说“如果a等于firstsecond”,你必须重复a==右手边的也:

a == 'first' || a == 'second' // education == 'yes' || education == 'y'
Run Code Online (Sandbox Code Playgroud)