user1200066
user1200066

Reputation: 71

Multiple conditions in C++ if statement

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;
}

Upvotes: 7

Views: 93437

Answers (5)

Robᵩ
Robᵩ

Reputation: 168596

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

You are so close to having the right answer. Let me give you two hints:

  1. The = operator is not the same as the == operator. = is the assignment operator. It evaluates its right-hand-side and stores the result in the variable named on its left-hand-side. You want ==, the equality operator. It tests to see if its right-hand side and its left-hand-side are equal.

  2. Use parenthesis ( ... ) to enforce your order-of-evaluation intention. You clearly mean to say "If either customerPackage is 'a' or it is 'A', and also hoursUsed is sufficiently large, then ...".

Try this line:

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

Upvotes: 5

Mr Lister
Mr Lister

Reputation: 46539

With the new problem you're having (in the other question you asked), you'll need some restructuring.

if ( (customerPackage == 'b' || customerPackage == 'B') && hoursUsed <= 20)

    amountDue = packageB;

    else
    {
        /* calculations */
    }

is not correct, that should be

if ( customerPackage == 'b' || customerPackage == 'B')
{
   if (hoursUsed <= 20)
   {
      amountDue = packageB;
   }
   else
   {
        /* calculations */
   }
}

Otherwise the first statement will only be executed when package=B AND hour=20, otherwise the calculations will be done in all other cases, like when package is A, or C.

Hope this helps!

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490028

Others have already helped you with the problem you've noticed. I'll start with a separate problem you apparently haven't noticed (yet):

    else
        overTime = packageA - hoursUsed;
        excessCharged = overTime * overPackageA;
        amountDue = packageA + excessCharged;

If you want all three of those statements controlled by the else, you need to enclose them in braces to create a compound statement:

else {
    overTime = packagA - hoursUsed;
    excessCharged = overTime * overPackageA;
    amountDue = packageA + excessCharged;
}

As it stands right now, your code is really:

    else
        overTime = packageA - hoursUsed;
    excessCharged = overTime * overPackageA;
    amountDue = packageA + excessCharged;

I.e., the computations for excessCharged and amountDue are carried out regardless of whether the condition in the if statement was true or false.

I'd also note that your switch statement doesn't really accomplish much:

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: ";

In particular, you take exactly the same action for all the cases (except the default). You can simplify this a bit by using fall-through cases:

switch (customerPackage) {
    case 'a':
    case 'A':
    case 'b':
    case 'B':
    case 'c':
    case 'C':
            cout << "Please enter the number of hours used: ";
            cin >> hoursUsed;
            break;
    default:
         cout << "Error " /* ... */;
}

Alternatively, you might consider something like:

static const char valid[] = "aAbBcC";

if (strchr(valid, userPackage)) {
    cout << "Please enter the number of hours used: ";
    cin >> hoursUsed;
}
else {
    std::cout << "Error: Please enter the customer's purchased package";
    std::cin >> userPackage;
}

Personally, however, I'd structure things a bit differently: first get one valid input, then get the next:

do { 
    std::cout << "Please enter the customer's purchased package (a, b, or c): ";
    std::cin >> userPackage;
} while (!strchr(valid, userPackage));

std::cout << "Please enter the number of hours used: ";
std::cin >> hoursUsed;

if (tolower(customerPackage == 'a') && hoursUsed >= 10)
// ...

Upvotes: 7

Adam Liss
Adam Liss

Reputation: 48280

You can use parentheses to specify the order in which the boolean operators are executed. You probably want to evaluate the || first, so you'd use:

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

The && is normally evaluated first by default, because it has higher precedence, so your code is equivalent to this:

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

Also, as noted in the comments, use == for comparison and = for assignment.

Upvotes: 0

Mark B
Mark B

Reputation: 96233

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)

Upvotes: 11

Related Questions