Doublecheck and help with my errors? I've been working on this same problem forever

Here's my code:

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, const char * argv[])
{
//Define variables
char package;	//types of package plans
int hours;		//number of hours used in the month
int month;		//month number (1-12)
double cost;	//cost per month
double costA;	//cost of package A
double costB;	//cost of package B
double costC;	//cost of package C
int maxHours;	//max amount of hours in the month
double savings;
	
//Explain package plans
cout << "Package A: For 9.95 per month 10 hours of access are provided." << endl;
cout << "           Additional hours are $2.00 per hour." << endl;
cout << "Package B: For 14.95 per month 20 hours of access are provided." << endl;
cout << "           Additional hours are $1.00 per hour." << endl;
cout << "Package C: For 19.95 per month unlimited access is provided.\n" << endl;
	
//Prompt user to enter ISP package
cout << "Enter ISP package plan: ";
cin >> package;
//Make sure user can't just enter any number, code or word - exit when invalid
//Will be invalid if package is not A, B or C
if (!((package == 'A') || (package == 'B') || (package == 'C')))
{
//Executes when the boolean expression is true
	cout << "Error: Invalid package." << endl;
	exit(1);
}
      
//Prompt user to enter month number
cout << "\nEnter month number (1 - 12): ";
cin >> month;
//Will be invalid if month is greater than 12 or less than 1
if ((month > 12) || (month < 1))
{
	//Executes when the boolean expression is true
    cout << "Error: Invalid month number." << endl;
	exit(1);     
}    

//Prompt user to enter number of hours used in the month
cout << "\nEnter number of hours used this month: ";
cin >> hours;
//Will be invalid if hours are 0 or less than 1
if (hours < 1)
{
//Executes when the boolean expression is true
	cout << "Error: Invalid number of hours";
	exit(1);
}    


if (package == 'A')
{
	if (hours <= 10)
{	
	cost = 9.95;	//declares and initializes cost of package A
	cout << "Your total payment for this month is $" << cost << endl;
}
else
{
	//executes when none of the above is true
	//declares and initializes cost of package A when there's additional hours
	cost = 9.95 + 2.00 * (hours - 10);
	cout << "Your total payment for this month is $" << cost << endl;
}
}
	
if (package == 'B')
{
	if (hours <= 20)
{
	cost = 14.95;	//declares and initializes cost of package B
	//if condition is true then print the following
	cout << "Your total payment for this month is $" << cost << endl;
}
	else
{
	//executes when none of the above is true
	//declares and initializes cost of package B when there's additional hours
	cost = 14.95 + 1.00 * (hours - 20);
	//if condition is true then print the following
	cout << "Your total payment for this month is $" << cost << endl;
}
}
   
if (package == 'C')
{
	cost = 19.95;	//declares and initializes cost of package C
	cout << '\n';
	//if condition is true then print the following
	cout << "Your total payment for this month is $" << cost << endl;
}
    
//Figure if they will save money with a particular package
if (package == 'A')
{
	costA = 9.95;	//declares and initializes cost of package A to figure out savings
}
else
{
//executes when none of the above is true
//declares and initializes cost of package A when there's additional hours
	costA = 9.95 + 2.00 * (hours - 10);
	cout << "You would have saved " << costB - costA << " if you chose " << package << " " << endl;
}
	
if (package == 'B')
{
	costB = 14.95; //declares and initializes cost of package B to figure out savings
	savings = costB - costC;	//expression to figure out savings if chose package C instead
}	
else
{
//executes when none of the above is true
	costB = 14.95 + 1.00 * (hours - 20); //declares and initializes cost of package B to figure out savings
	savings = costB - costC;	//expression to figure out savings if chose package C instead    
cout << "You would have saved $" << costB - costC << " if you chose Package B" << endl;
}
    
if (package == 'C')
{
	costC = 19.95;  //declares and initializes cost of package B to figure out savings
}
	
if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
{
	maxHours = (31 * 24);
}
else if (month == 2)
{
	maxHours = (28 * 24);
}
	
if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
{
	maxHours = (30 * 24);
}
	
//Setting field width and precision
cout << "\nISP Cost by Student\n";
cout << fixed << showpoint << left << setprecision(2) << "Package " << setw(10) << package << endl;
cout << fixed << showpoint << left << setprecision(2) << "Cost $" << setw(10) << cost << endl;
cout << fixed << showpoint << left << setprecision(2) << "Hours " << setw(10) << hours << endl;    
	return 0;
	
}


We're supposed to use If logic to compute cost for ISP service depending on the user’s plan. I keep working on this and can't figure out what's wrong. The output should display basically the bill for the student with what package was entered, hours used, savings if had picked a different package and total cost.
Last edited on
Anyone?
Could you maybe first place your code in [code] ... [(/)code]?
? What?
Your code is very hard to read. You should first place your code in code brackets found on the right hand side of the text editor. Just select your text and click the little picture that looks like <>
What program do you use? I have a Mac. I have CodeRunner and Xcode
He means for the forum. You may notice in your "edit post" button there is a little 3x4 table of formatting options, with Format: written above it. The icon that looks like a <> symbol is in the first row and first column. This is because it is very hard to read your code at the moment, due to the code being non-indented and not using fixed-size characters.

Ohh ok, fixed it. Thank you!
A little thing: Your exit(1); statements are BEFORE you print the error messages, which means that the program will close before they actually get printed! Try moving the exit statement to the end of the block.

Why do you have "maxHours"? You don't even use if for anything...

Also, your if statements are a bit wierd. For example, you have:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (package == 'A') {    // If package 'A' was chosen
    if (hours <= 10)     // If the number of hours isn't greater than 10
        cost = 9.95;      // Set the cost to 9.95

    // Output the cost.
    // Here, the cost will either be 9.95, or some random number
    // (maybe 0 based on your compiler). This is because you haven't
    // initialized cost yet.
    cout << "Your total payment for this month is $" << cost << endl;
} else {         // Else, if the package is NOT 'A'
    cost = 9.95 + 2.00 * (hours - 10);
    cout << "Your total payment for this month is $" << cost << endl;
}


See what you did there? You have the cost being calculated for when the chosen package was NOT 'A', so that it would calculate only if package 'B' or 'C' was chosen.

Instead, you may want to do this:

1
2
3
4
5
6
7
8
9
10
11
if (package == 'A') {
    if (hours <= 10)
        cost = 9.95;
    else
        cost = 9.95 + 2.00 * (hours - 10);

    cout << "Your total payment for this month is $" << cost << "." << endl;
}
if (package == 'B') {
    // same thing
}


You shouldn't be checking for their package choice when finding the best options, because you want to find the actual best choice, so you should work it out for every option.

Also, with your printing out at the end, you have neglected to insert a space between cost and hours, which is why you got 0.0014.
Last edited on
With your if statement for 'A' you missed some brackets but you got it right for 'B'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (package == 'A')
{
	if (hours <= 10)
{/// missing bracket
	cost = 9.95;	//declares and initializes cost of package A
	cout << "Your total payment for this month is $" << cost << endl;
}
else
{
//executes when none of the above is true
//declares and initializes cost of package A when there's additional hours
	cost = 9.95 + 2.00 * (hours - 10);
	cout << "Your total payment for this month is $" << cost << endl;
}
}/// missing bracket 
Ok, I think I fixed those parts. Now I'm not sure if each package is running correctly. Should I be getting negative numbers for savings? Like for C:

Enter ISP package plan: C

Enter month number (1 - 12): 2

Enter number of hours used this month: 12

Your total payment for this month is $19.95
You would have saved -13.95 if you chose C
You would have saved $6.95 if you chose Package B

ISP Cost by Student
Package C
Cost $19.95
Hours 12

Should I set it up differently? The teacher's example for Package A shows
Package A
Hours Used: 20
Monthly Bill: $29.95

"If you had used Package B the bill would be $14.95 for a savings of 15.00!
If you had used Package C the bill would be $19.95 for a savings of 10.00!"

I don't think I have the savings right.

To calculate the savings calculate the cost of all packages (costA, costB, costC) from the input hours.

Then calculate the savings by doing cost - costA, cost-costB, cost-costC.
Then depending what package the user chose print out the savings for the other two packages but if any of these results are negative it is not a saving so don't print it.

In your program you use costB and costC variables before they have been given a value.
Last edited on
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
//Figure if they will save money with a particular package
if (package == 'A')
{
	if (hours <= 10)
{
	costA = 9.95;	//declares and initializes cost of package A to figure out savings
	costB = 14.95;	//declares and initializes cost of package B to figure out savings
	costC = 19.95;	//declares and initializes cost of package C to figure out savings
	cout << "You would have saved $" << cost - costB << " if you chose Package B" << endl;
	cout << "You would have saved $" << cost - costC << " if you chose Package C" << endl;
}
	
	else
{
	//executes when none of the above is true
	//declares and initializes cost of package A when there's additional hours
	costA = 9.95 + 2.00 * (hours - 10); //declares and initializes cost of package A to figure out savings
	costB = 14.95 + 1.00 * (hours - 20); //declares and initializes cost of package B to figure out savings
	costC = 19.95;	//declares and initializes cost of package C to figure out savings
	cout << "You would have saved $" << cost - costB << " if you chose Package B" << endl;
	cout << "You would have saved $" << cost - costC << " if you chose Package C" << endl;
}
}
	
if (package == 'B')
{
	if (hours <= 20)
{
	costB = 14.95; //declares and initializes cost of package B to figure out savings
	costA = 9.95;	//declares and initializes cost of package A to figure out savings 
	costC = 19.95;	//declares and initializes cost of package C to figure out savings
	
	//expression to figure out savings if chose package A instead 
	cout << "You would have saved $" << cost - costA << " if you chose Package A" << endl;
	//expression to figure out savings if chose package C instead
	cout << "You would have saved $" << cost - costC << " if you chose Package C" << endl;
}	
	else
{
	//executes when none of the above is true
	//declares and initializes cost of package B when there's additional hours
	costB = 14.95 + 1.00 * (hours - 20); 	//declares and initializes cost of package B to figure out savings
	costA = 9.95 + 2.00 * (hours - 10);	//declares and initializes cost of package A to figure out savings
	costC = 9.95;   //declares and initializes cost of package C to figure out savings

	//expression to figure out savings if chose package A instead     
	cout << "You would have saved $" << cost - costA << " if you chose Package A" << endl;
	//expression to figure out savings if chose package C instead 
	cout << "You would have saved $" << cost - costC << " if you chose Package C" << endl;
}
}



I don't know...this stuff confuses me. Am I on the right track? It doesn't seem that the calculations are coming out correctly.
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, const char * argv[])
{
//Define variables
char package;	//types of package plans
int hours;		//number of hours used in the month
int month;		//month number (1-12)
double cost;	//cost per month
double costA;	//cost of package A
double costB;	//cost of package B
double costC;	//cost of package C
int maxHours;	//max amount of hours in the month

//Explain package plans
cout << "Package A: For 9.95 per month 10 hours of access are provided." << endl;
cout << "           Additional hours are $2.00 per hour." << endl;
cout << "Package B: For 14.95 per month 20 hours of access are provided." << endl;
cout << "           Additional hours are $1.00 per hour." << endl;
cout << "Package C: For 19.95 per month unlimited access is provided.\n" << endl;
	
//Prompt user to enter ISP package
cout << "Enter ISP package plan: ";
cin >> package;
//Make sure user can't just enter any number, code or word - exit when invalid
//Will be invalid if package is not A, B or C
if (!((package == 'A') || (package == 'B') || (package == 'C')))
{
	//Executes when the boolean expression is true
	cout << "Error: Invalid package." << endl;
	//exits program
	exit(1);
}
      
//Prompt user to enter month number
cout << "\nEnter month number (1 - 12): ";
cin >> month;
//Months can't be more than 12 or less than 1 - Will be invalid 
if ((month > 12) || (month < 1))
{
	//Executes when the boolean expression is true
    cout << "Error: Invalid month number." << endl;
	//exits program
	exit(1);     
}    

//Prompt user to enter number of hours used in the month
cout << "\nEnter number of hours used this month: ";
cin >> hours;

if (hours < 1)
{
	//Executes when the boolean expression is true
	//If the hours are less than one, it would be 0 or a negative. That is invalid.
	cout << "Error: Invalid number of hours";
	//exits program
	exit(1);
}    


if (package == 'A') 
{
	if (hours <= 10) 
{	
	cost = 9.95;	//declares and initializes cost of package A
	cout << "Your total payment for this month is $" << cost << endl;
}
else 		//executes when none of the above is true
{	
	//declares and initializes cost of package A when there's additional hours
	cost = 9.95 + 2.00 * (hours - 10);
	cout << "Your total payment for this month is $" << cost << endl;
}
}
	
if (package == 'B')
{
	if (hours <= 20)
{
	cost = 14.95;	//declares and initializes cost of package B
	//if condition is true then print the following
	cout << "Your total payment for this month is $" << cost << endl;
}
	else	//executes when none of the above is true
{
	//declares and initializes cost of package B when there's additional hours
	cost = 14.95 + 1.00 * (hours - 20);
	//if condition is true then print the following
	cout << "Your total payment for this month is $" << cost << endl;
}
}
   
if (package == 'C')
{
	cost = 19.95;	//declares and initializes cost of package C
	cout << '\n';
	//if condition is true then print the following
	cout << "Your total payment for this month is $" << cost << endl;
}

//Figure if they will save money with a particular package
if (package == 'A')
{
	if (hours <= 10)
{
	costA = 9.95;	//declares and initializes cost of package A to figure out savings
	costB = 14.95;	//declares and initializes cost of package B to figure out savings
	costC = 19.95;	//declares and initializes cost of package C to figure out savings
	
	if (cost - costB > 0)			//if cost - the cost of package B is greater than zero the user will have saved money
	{
		//expression to figure out savings if chose package B instead 
		cout << "You would have saved $" << cost - costB << " if you chose Package B" << endl;
	}
	
	if (cost - costC > 0)			//if cost - the cost of package C is greater than zero the user will have saved money
	{
		//expression to figure out savings if chose package C instead 
		cout << "You would have saved $" << cost - costC << " if you chose Package C" << endl;
	}
}	
	else	//executes when none of the above is true
{
	
	//declares and initializes cost of package A when there's additional hours
	costA = 9.95 + 2.00 * (hours - 10); //declares and initializes cost of package A to figure out savings
	costB = 14.95 + 1.00 * (hours - 20); //declares and initializes cost of package B to figure out savings
	costC = 19.95;	//declares and initializes cost of package C to figure out savings
	
	if (cost - costB > 0)			//if cost - the cost of package B is greater than zero the user will have saved money
	{
		//expression to figure out savings if chose package B instead 
		cout << "You would have saved $" << cost - costB << " if you chose Package B" << endl;
	}
	
	if (cost - costC > 0)			//if cost - the cost of package C is greater than zero the user will have saved money
	{
		//expression to figure out savings if chose package C instead 
		cout << "You would have saved $" << cost - costC << " if you chose Package C" << endl;
	}
	
}
}

	
if (package == 'B')
{
	if (hours <= 20)
{
	costB = 14.95; //declares and initializes cost of package B to figure out savings
	costA = 9.95;	//declares and initializes cost of package A to figure out savings 
	costC = 19.95;	//declares and initializes cost of package C to figure out savings
	
	if (cost - costA > 0)		//if cost - the cost of package A is greater than zero the user will have saved money
	{
	//expression to figure out savings if chose package A instead 
	cout << "You would have saved $" << cost - costA << " if you chose Package A" << endl;
	}
	 
	if (cost - costC > 0)		//if cost - the cost of package A is greater than zero the user will have saved money
	{
	//expression to figure out savings if chose package C instead
	cout << "You would have saved $" << cost - costC << " if you chose Package C" << endl;
	}
}	
	else	//executes when none of the above is true
{
	
	//declares and initializes cost of package B when there's additional hours
	costB = 14.95 + 1.00 * (hours - 20); 	//declares and initializes cost of package B to figure out savings
	costA = 9.95 + 2.00 * (hours - 10);	//declares and initializes cost of package A to figure out savings
	costC = 9.95;   //declares and initializes cost of package C to figure out savings
	
	if (cost - costA > 0)		//if cost - the cost of package A is greater than zero the user will have saved money
	{
	//expression to figure out savings if chose package A instead     
	cout << "You would have saved $" << cost - costA << " if you chose Package A" << endl;
	} 
	
	if (cost - costC > 0)		//if cost - the cost of package C is greater than zero the user will have saved money
	{
	//expression to figure out savings if chose package C instead 
	cout << "You would have saved $" << cost - costC << " if you chose Package C" << endl;
	}
}	
}

if (package == 'C')
{
	costC = 19.95;  //declares and initializes cost of package C to figure out savings
}


//Makes sure that savings aren't listed as negative and displays as $0
if (package == 'A')
{
	if (cost - costB <= 0)			//if cost - the cost of package B is less than or equal to zero the user won't be saving any money
	{
		cout << "You would have saved $0 if you chose Package B" << endl;
	}
	
	if (cost - costC <= 0)			//if cost - the cost of package C is less than or equal to zero the user won't be saving any money
	{
		cout << "You would have saved $0 if you chose Package C" << endl;
	} 
	
}

//Makes sure that savings aren't listed as negative and displays as $0
if (package == 'B')
{
	if (cost - costA <= 0)			//if cost - the cost of package A is less than or equal to zero the user won't be saving any money
	{
		cout << "You would have saved $0 if you chose Package A" << endl;
	} 
	if (cost - costC <= 0)			//if cost - the cost of package C is less than or equal to zero the user won't be saving any money
	{
		cout << "You would have saved $0 if you chose Package C" << endl;
	}
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//these months each have 31 days	
if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
{
	maxHours = (31 * 24);	//multiplied by 24 gives the maximum amount of hours in each month
}
else if (month == 2)	//month has 28 days
{
	maxHours = (28 * 24); 	//multiplied by 24 gives the maximum amount of hours in the month
}
	
if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) //months each have 30 days
{
	maxHours = (30 * 24);	//multiplied by 24 gives the maximum amount of hours in each month
}
	
//Setting field width and precision
//displays chart of package, cost and hours
cout << "\nISP Cost by Student\n";
cout << fixed << showpoint << left << setprecision(2) << "Package " << setw(10) << package << endl;
cout << fixed << showpoint << left << setprecision(2) << "Cost $" << setw(10) << cost << endl;
cout << fixed << showpoint << left << setprecision(2) << "Hours " << setw(10) << hours << endl;    
	return 0;
	
}
Last edited on
Topic archived. No new replies allowed.