Multi condition if statement C ++

I am very new to the C ++ programming concept. I want to have a multi condition if condition using || or, and && in one statement. When I ask my fellow professor about it. She said that it was possible, and then offended my limited knowledge on this issue. All the examples that I have access to show multi && and only one showing ||. It does not show that they are used together. I would like to know how to make the line work. I will attach the code that 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 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 purchased package: ";
        cin >> customerPackage;
    }    

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

, && , ||, . , == (=):

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

+10

, . , , , ():

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

, else, :

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

, :

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

I.e., excessCharged amountDue , if .

, switch :

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

, ( ). , :

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 " /* ... */;
}

- :

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 purchased package";
    std::cin >> userPackage;
}

, , -: , :

do { 
    std::cout << "Please enter the customer 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)
// ...
+6
if ( customerPackage ='a' || customerPackage ='A' && hoursUsed >= 10)

. :

  • = ==. = - . , . ==, . , .

  • ( ... ) . : " customerPackage" a ", " A ", hoursUsed , ...".

:

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

, . , ||, :

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

&& , , :

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

, , == = .

0

With the new problem you are facing (in another question that you asked), you will need a restructuring.

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

    amountDue = packageB;

    else
    {
        /* calculations */
    }

wrong it should be

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

Otherwise, the first instruction will be executed only when package = B AND hour = 20, otherwise, calculations will be performed in all other cases, for example, when the package is A or C.

Hope this helps!

0
source

All Articles