C ++ if语句中的多个条件 [英] Multiple conditions in C++ if statement

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

问题描述

我对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 professor 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 like 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;
}

推荐答案

您的问题是&&的优先级高于||,因此您需要括号.如评论中所述,您还需要使用==而不是赋值(=):

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)

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

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