7

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;
}
Waqar
  • 8,558
  • 4
  • 35
  • 43
user1200066
  • 71
  • 1
  • 1
  • 3
  • 6
    Do comparison with `==`, not `=`. And be sure to use `()` to disambiguate your conditions. Otherwise.. what's wrong? Calling your code a "problem area" doesn't give us much to go on in terms of figuring out what you want it to do that it doesn't. – Lightness Races in Orbit Feb 09 '12 at 16:11
  • comparison operator is *==* on c/c++ – Gigi Feb 09 '12 at 16:12
  • 3
    Also, do yourself a favor and place parenthesis around each compare, just so that you and everyone else is sure of the order of the && and || above. – Michael Dorgan Feb 09 '12 at 16:13
  • Possible duplicate of [Can you use 2 or more OR conditions in an if statement?](https://stackoverflow.com/q/8781447/608639) – jww Sep 24 '18 at 01:17

5 Answers5

11

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)

Mark B
  • 95,107
  • 10
  • 109
  • 188
7

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)
// ...
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • First off thank you to all who are helping me. I have made changes base off the information shared here and give credit where it is due. I am almost done with the program. I am having two issues. one is the problem is going thru all the packages and computing the wrong amounts. I have gone back and made the suggested changes for the issue of not falling out of if statement when needed. I know it is some small step i am missing. Again thanks to all. – user1200066 Feb 09 '12 at 18:07
5
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)
Waqar
  • 8,558
  • 4
  • 35
  • 43
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

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.

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
  • thank you. this is very useful information and in the event I will need to call for help again I will stride to be more detailed. – user1200066 Feb 09 '12 at 16:54
0

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!

Community
  • 1
  • 1
Mr Lister
  • 45,515
  • 15
  • 108
  • 150