Nested if statements and output are not what should be diplayed

I am extremely new to the concept of computer programming for C++. How my current assignment I'm asked to do three separate times. The first part seems to function correctly. The second part nine currently having trouble with is to calculate savings and displayed the savings for the additional packages with the final package showing no savings. The final step is to have the user spell the name of each month and calculate will be a Mountain Dew and savings based on the maximum number of hours for each month. I am requesting help in getting the second part to be used by the program because it looks to me that it is being ignored. I will like to see a general layout on how to do the months has a switch or a nested if statements. I appreciate all help in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

// Purpose: Ask which package the customer has purchased and how many hours were
//          used for the billing cycle. It should then display the total amount
//          due based on input validation. Input Validaton: be sure the user
//          onlys select package a,b,or c. The number of usage hours cannot
//          exceed 744. Program should display saving for switching a cheaping
//          package to a more expensive package.
// *****************************************************************************

#include <iostream>
#include <cstring>
#include <iomanip>

using namespace std;

int main()
{
    // Declare Constants
    const int MAXIMUM_HOURS = 744;
    const double PACKAGE_A = 9.95,
                 PACKAGE_A_HOURS = 10,
                 OVER_PACKAGE_A =2.00,
                 PACKAGE_B = 14.95,
                 PACKAGE_B_HOURS = 20,
                 OVER_PACKAGE_B =1.00,
                 PACKAGE_C = 19.95;
                 
    // Declare Variables
    char customerPackage;
    double hoursUsed =0,
           overTime =0,
           amountDue =0,
           packageBDifference =0,
           bSaveHours = 0,
           packageCDifference =0,
           cSaveHours = 0,
           excessCharged = 0;
           

    // prompts the user to enter the customer's package and reads the input          
    cout << "Please enter the customer's package: ";
    cin >> customerPackage;
    
    // validates the input
    if (customerPackage !='a' && customerPackage !='A' && 
        customerPackage !='b' && customerPackage !='B' &&
        customerPackage !='c' && customerPackage !='C')
    {
        cout << "\nError. package does not exist\n"
             << "Please enter the customer's purchased package plan: ";
        cin >> customerPackage;
    }

    // prompts user to enter the hours used for the current billing cycle and
    // reads the input
    cout << "Please the number of hours used for the current cycle: ";
    cin >> hoursUsed;                                   
    
    // validates the input
    if (hoursUsed >= MAXIMUM_HOURS || hoursUsed < 0)      
    {
        cout << "\nError. hours are outside of maximum range\n"
             << "Please enter the current month's hours usage: ";
        cin >> hoursUsed;
    } 
    
    // computes the amount due based on package type and hours usage          
    if ((customerPackage == 'a' || customerPackage == 'A')
        && hoursUsed <= PACKAGE_A_HOURS)
     
    {
        amountDue = PACKAGE_A;
    }
    else if((customerPackage == 'a' || customerPackage == 'A')
            && hoursUsed > PACKAGE_A_HOURS)
    {
        overTime = hoursUsed -PACKAGE_A_HOURS;
        excessCharged = overTime * OVER_PACKAGE_A;
        amountDue = PACKAGE_A + excessCharged;
        
     }     
    else if ((customerPackage == 'b' || customerPackage == 'B')
            && hoursUsed <= PACKAGE_B_HOURS)
     
    {
        amountDue = PACKAGE_B;
    }
    else if((customerPackage == 'b' || customerPackage == 'B')
            && hoursUsed > PACKAGE_B_HOURS)
    {
        overTime = hoursUsed -PACKAGE_B_HOURS;
        excessCharged = overTime * OVER_PACKAGE_B;
        amountDue = PACKAGE_B + excessCharged;
    } 
    else
   {
        amountDue = PACKAGE_C;
    } 
    
    // computes the available savings for different package types        THIS IS WHERE IT GOES WRONG
     if ((hoursUsed < PACKAGE_A_HOURS && customerPackage =='a' ||
          customerPackage =='A'))
     {
        packageBDifference = PACKAGE_B - PACKAGE_A;
        bSaveHours = packageBDifference / PACKAGE_A_HOURS;
        packageCDifference = PACKAGE_C - PACKAGE_B_HOURS;
        cSaveHours = PACKAGE_C / PACKAGE_B_HOURS; 
     }
    else if ((customerPackage == 'b' ||customerPackage == 'B')
            && hoursUsed < PACKAGE_B)
    {
        packageCDifference = PACKAGE_C - PACKAGE_B_HOURS;
        cSaveHours = PACKAGE_C / PACKAGE_B_HOURS;
    
    }
    
    //outputs to the display and the amount charged and savings
    cout << fixed << showpoint <<setprecision(2);
    cout << "\nCustomer is charged " << amountDue << endl; 
    
    if ( bSaveHours < 0)
    {
        cout << "With the B package customer can save: " << bSaveHours << endl;
        cout << "With the C package customer can save: " << cSaveHours << endl;
    }
    else        
    system("pause");
    return 0;
}

Last edited on
Not sure here which part of your program is the part that's not working. Could you add comments and/or output to point out the lines you're having difficulty with?
for one, quick tip, save your self time and possible logic errors (the || parts that need to be enclosed by parentheses from the && parts) with case of the package name ("a" and "A") by using strings and doing:

1
2
3
4
cin >> customerPackage;

customerPackage = MakeLower(customerPackage);

where the MakeLower function is:
1
2
3
4
5
6
std::string MakeLower(std::string str) {
    for(std::string::size_type i = 0; i < str.size(); ++i) {
        str[i] = tolower(str[i]);
    }
   return str;
}

and you will need to include <stdio> or <iomanip> or some header file, i forget.

setprecision(2) should be placed before the variable you are about to set its precision for.

for the switch part:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::string month;

swtich (month)
{
  case "January":
     DoWhatever();
     break;
  case "February":
     DoWhatever();
     break:
  ...
  default:
     NotAMonthWhatever();
     break;
}


as for the part that is being ignored, please elaborate on where this is occurring. what you said above made little sense when I read it.

Topic archived. No new replies allowed.