C++ Assignment help

Hi all. I am fairly new to programming. so I need a little help. I was given this assignment today and can't seem to figure out what I am doing wrong. Here is the assignment and my code is below it. I have three different files. Was hoping someone could look over it and give me some direction. Thanks.

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

Write a program that calculates a customer’s monthly charges.
Implement with the following functions in a separate functions.cpp file:
getPackage
validPackage
getHours
validHours
calculatePkg_A
calculatePkg_B
calculatePkg_C
calculateCharges
showBill
Validate all input.
Demonstrate valid and invalid cases.
Show console dialog. It must be readable.

Here is what I have so far.

1
2
3
4
5
6
7
8
9
10
11
//functions.h

char getValidPkg();

char getValidHrs();

char calculatePkg_A();

char calculatePkg_B();

char calculatePkg_C();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

#include "Header.h"

int main()
{
	char pkg;
	pkg = getValidPkg();
	cout<<"Pkg: "<<pkg<<endl;
	
	int hrs;
	hrs = getValidHrs();
	cout<<"Hrs: "<<hrs<<endl;
	



	
}//endmain  


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
#include <iostream>
using namespace std;

bool isValidPkg(	char p	)								//checkpkg
{
	return (p>='A' && p<='C') || (p>='a' && p<='c');
}

char getValidPkg()											//getpkg
{
	char p=0;

	do
	{
		cout<<"Enter pkg: ";
		cin>>p;
	}
	while(	!isValidPkg(p)	);

	return p;
	
}

bool isValidHrs(	int h	)								//checkhrs
{
	return (h>=0 && h<=720);
}

char getValidHrs()											//gethrs
{
	int h;

	do
	{
		cout<<"Enter hours: ";
		cin>>h;
	}
	while(	!isValidHrs(h)	);

	return h;
}
Last edited on
Nice job! This is very very good code. What problems are you having?
I am just not sure what my next step is. Like whenever I run it, it runs kinda smooth, but I am just wondering what should I do now.
Write calculatePkg_A(), calcualtePkg_B() and calculatePkg_C(). Your prototypes aren't right: these should take the number of hours as a parameter and return the charge. For example:
double calculatePkg_A(int hours);
where should I write them?
Whenever I run the code, this is what I get.

Enter pkg: a
Pkg: a
Enter hours: 50
Hrs: 50
Press any key to continue . . .
You need to write the functions calculate_Pkg_A(). calcualtePkg_B() and calculatePkg_C(). Then write calculateCharges(). This will take the package as a parameter and will call one of the calculate_Pkg_X() functions to calculate the charge for the given package.
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
#include <iostream>
using namespace std;
void showBill(double chg);
double calculatePkg_A(int hrs);
double calculatePkg_B(int hrs);
double calculatePkg_C(int hrs);
double calculateCharges(char pkg, int hrs);

bool isValidPkg(	char p	)								//checkpkg
{
	return (p>='A' && p<='C') || (p>='a' && p<='c');
}

char getValidPkg()											//getpkg
{
	char p=0;

	do
	{
		cout<<"Enter pkg: ";
		cin>>p;
	}
	while(	!isValidPkg(p)	);

	return p;
	
}

bool isValidHrs(	int h	)								//checkhrs
{
	return (h>=0 && h<=720);
}

char getValidHrs()											//gethrs
{
	int h;

	do
	{
		cout<<"Enter hours: ";
		cin>>h;
	}
	while(	!isValidHrs(h)	);

	return h;
}
double calculatePkg_A(int hrs)
{
	int baseCost = 15;
	int baseHrs = 50;
	double extraCost = 2.0;
	double cost  =  hrs > baseHrs ? baseCost + ( hrs -
		 	baseCost ) * extraCost  :  baseCost ;
	return cost;
}
double calculatePkg_B(int hrs)
{
	int baseCost = 20;
	int baseHrs = 100;
	double extraCost = 1.50;
	double cost  =  hrs > baseHrs ? baseCost + ( hrs -
		 	baseCost ) * extraCost  :  baseCost ;
	return cost;
}
double calculatePkg_C(int hrs)
{
	int baseCost = 25;
	int baseHrs = 150;
	double extraCost = 1.00;
	double cost  =  hrs > baseHrs ? baseCost + ( hrs -
		 	baseCost ) * extraCost  :  baseCost ;
	return cost;
}
double calculateCharges(char p, int hrs)
{
  	double chg;
 	 switch(p)
	{
	case 'A':
    case 'a': chg = calculatePkg_A(hrs);
	break;
	case 'B':
	case 'b':  chg = calculatePkg_B(hrs);
	break;
	default: chg = calculatePkg_C(hrs);
	}
	return chg;

}
void showBill(double chg)
{
	cout << "You owe: "
	     << chg << endl;
}


Kinda like this?
Exactly!!!! Nice job.

Now you just need to write the main program which will call these functions to do the work.
Topic archived. No new replies allowed.