Help with a C++ Homework Problem

I am having issues with a C++ homework problem given to me this afternoon. MY program is telling me i havent properly declared 'a,b,c,A,B,C.' The problem description is :

An Internet service provider has three different subscription
packages for its customers:

Package A: For $15 per month with 50 hours of access provided.
Additional hours are $2.00 per hour over 50 hours.
Assume usage is recorded in one-hour increments,

Package B: For $20 per month with 100 hours of access provided.
Additional hours are $1.50 per hour over 100 hours.

Package C: For $25 per month with 150 hours access is provided.
Additional hours are $1.00 per hour over 150 hours

Assume a 30-day billing cycle.

He wanted to include a "bool flag" with a 'while' loop to stop the user from putting in a lower case letter for the one of the options. Below is my code, i am new so I am sure there is issues. Any help would be appreciated, thank you 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
//Internet Service Providers Program3

#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
	//Variables
	char pkg;
	int hrs(0);
	double total, charges(0);

	//Bool Flag
	bool flag = true;
		while(flag)
		{
			cout<<"Enter the Internet Package you choose: "<<endl;
			cin>>pkg;
			if (pkg == a || b || c )
			{
				flag = false;
				cout<<"Invalid Package Option\n\n";
			}
			else if (pkg == A || B || C)
			{
				flag = true;
				cout<<"Enter the number of hours you need for access: "<<endl;
				cin>>hrs;
			}
			//Plan A
			else if (pkg == A && hrs>=50)
			{
				charges = 15.00 * 50;
				total = charges + (hrs-50)*2.00;
				cout<<"Your total for Plan A is: $ "<<total
					<<endl<<endl;
			}
			//Plan B
			else if (pkg == B && hrs>=100)
			{
				charges=20.00*100;
				total = charges + (hrs-100)*1.50;
				cout<<"Your total for Plan B is: $ "<<total
					<<endl<<endl;
			}
			//Plan C
			else if (pkg == C && hrs>=150)
			{
				charges=25.00*150;
				total = charges + (hrs-150*1.00);
				cout<<"Your total for Plan C is: $ "<<total
					<<endl<<endl;
			}
			else;
			}
	return 0;
		
}


Last edited on
You haven't declared a,b,c or A,B,C. If you want a comparison to a character, you need to tell the compiler it is a character.

a <- This the compiler thinks is referencing a variable, a.

'a' <- This the compiler thinks is referencing the character 'a'.
Ah okay , Thank you 'Mats' - did everything else in the while loop look correct?

It built - but when i test the program even using an "A" its saying ::

Enter the Internet Package you choose:
A
Invalid Package Option

Press any key to continue . . .

It doesn't go on to prompt the user to enter the hours..
It should be 'a' (single quotes). "a" is a string and will not work. You also want to have each condition in the if/else sections checked properly.
1
2
3
4
 
if(pkg == 'a' || 'b')
{
}

Should look like:
1
2
3
4
 
if(pkg == 'a' || pkg == 'b')
{
}


They should read like that, otherwise, they will not work. Also, your while loop needs to check some value of flag. while(flag == true) or while(flag == false)

Also, as a little sidenote, using "\n" is better than using endl when you don't have a reason to flush the stream.

1
2
cout<<"Your total for Plan A is: $ "<<total
					<<endl<<endl;


Is better as:
cout<<"Your total for Plan A is: $ "<< total << "\n\n";
Last edited on
Topic archived. No new replies allowed.