多条件if语句c ++ [英] multi condition if statement c++

查看:382
本文介绍了多条件if语句c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++编程的概念很陌生。我希望有一个多条件if语句使用||或者和&&在一个声明中。当我问我的大学忏悔者时。她说这是可能的,然后侮辱了我对这个问题的有限知识。我可以访问的所有示例都显示了多个&&声明,只有一个显示||。它没有显示它们一起使用。我想学习如何使线路正常工作。我会附上我的代码。问题区域是最后一点编码。

I am very new to the concept of programming in C++. I am wanting to have a multi condition if statement using the || or and the and && in one statement. When I ask my college prfessor about it. She told it was possible and then insulted my limited knowledge on the subject. All examples I have access to show a multi && statement and only one showing the ||. It does not show them being used together. I would to learn how to get the line working. I will attach the code I have. The problem area is the last bit of coding.

# include <iostream>
# include <cstring>

using namespace std;

main()
{

    const int maximumHours = 774;
    char customerPackage;
    double hoursUsed = 0,
           packageA = 9.95,
           packageB = 14.95,
           packageC = 19.95,
           overPackageA = 2.00,
           overPackageB = 1.00,
           overTime = 0,
           amountDue = 0,
           excessCharged = 0;

    cout << "Please enter the customer's package: ";
    cin >> customerPackage;

    switch (customerPackage)
    {
        case 'a' :
            cout << "Please enter the number of hours used: ";
                cin >> hoursUsed;
            break;

        case 'A' :
            cout << "Please enter the number of hours used: ";
                cin >> hoursUsed;
            break;

        case 'b' :
            cout << "Please enter the number of hours used: ";
                cin >> hoursUsed;
            break;

        case 'B' :
            cout << "Please enter the number of hours used: ";
                cin >> hoursUsed;
            break;

        case 'c' :
            cout << "Please enter the number of hours used: ";
                cin >> hoursUsed;
            break;

        case 'C' :
            cout << "Please enter the number of hours used: ";
            cin >> hoursUsed;
            break;        
        default: cout << "Error." 
            << " Please enter the customer's purchased package: ";
        cin >> customerPackage;
    }    

    if ( customerPackage ='a' || customerPackage ='A' && hoursUsed >= 10)           
        amountDue = packageA;
        else
            overTime = packageA - hoursUsed;
            excessCharged = overTime * overPackageA;
            amountDue = packageA + excessCharged;
}


推荐答案

你的问题是&& 的优先级高于 || 所以你需要parens。如评论中所述,您还需要使用 == 而不是赋值( = ):

Your problem is that && has higher precedence than || so you need parens. As noted in a comment you also need to use == instead of assignment (=):

if((customerPackage =='a'|| customerPackage =='A')&& hoursUsed> = 10)

这篇关于多条件if语句c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆