Help Function assignment

Hello all, I'm pretty new to programming and I'm not very good at it. But I have this assignment and I can't figure out whats wrong with my program. Here is the assignment and my code is below it.
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
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
#include <iostream>
#include <iomanip>
using namespace std;

char getValidPkg();

char getValidHrs();

char calculatePkg_A();

char calculatePkg_B();

char calculatePkg_C();


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;
}
Last edited on
closed account (48T7M4Gy)
What you need to do is tell us what you are getting when you run your program that is not up to your expectation(s).

It's fairly clear what it is supposed to do from your preamble but ...
closed account (48T7M4Gy)
For a start you don't have

1
2
3
4
int main()
{
      return 0;
}


That is the bare minimum. If you don't have at least that absolutely nothing will happen. In fact it is a C++ error.

Last edited on
I've made the changes for
1
2
int main()
{ return 0;}


but 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 . . .
closed account (48T7M4Gy)
OK so what extra output do you want? :)
closed account (48T7M4Gy)
Also you will have to update your first post with the missing main otherwise we can't help you.
So i want it to calculate the charges of these
Test Case Package Hours
1 A 50
2 a 51
3 B 100
4 b 101
5 C 149
6 c 251
7 e 720
8 c 722
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
#include <iostream>
using namespace std;

char getValidPkg();
char getValidHrs();
char calculatePkg_A();
char calculatePkg_B();
char calculatePkg_C();
int main()
{
	char pkg;
	pkg = getValidPkg();
	cout << "Pkg: " << pkg << endl;

	int hrs;
	hrs = getValidHrs();
	cout << "Hrs: " << hrs << endl;



return 0;

}
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;
}
closed account (E0p9LyTq)
Lamshark wrote:
but whenever I run the code, this is what I get.


With the way you have written your main() function that is the output you should get:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
   char pkg;
   pkg = getValidPkg();
   cout << "Pkg: " << pkg << endl;

   int hrs;
   hrs = getValidHrs();
   cout << "Hrs: " << hrs << endl;

   return 0;
}
How should I re-write my main function to get the calculations i need?
Topic archived. No new replies allowed.