我似乎对基本的if / else语句有麻烦:/ [关闭]

问题描述 投票:-1回答:1

IDEA(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.";
    }
}

问题:

现在,如果我输入我未接受全日制教育且不超过21岁,则输出为“您不符合条件,因为您超过21岁。”从技术上讲是正确的,但它应该给我“您不是合格,因为您尚未接受全日制教育,并且您已超过21岁。代替!

注意事项:

  • 我的#include语句是从屏幕截图中删除的,但是不用担心,我知道它们很好。
  • [所有“ else if”语句本来只是“ if”,但是我以这种方式使它们尝试解决此问题..显然没有用。
c++ output
1个回答
3
投票

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

a == 'first' || 'second' // education == 'yes' || 'y'

为了说“如果a等于firstsecond”,则必须在右侧也重复a==

a == 'first' || a == 'second' // education == 'yes' || education == 'y'
© www.soinside.com 2019 - 2024. All rights reserved.