Hours keep coming up as zero.

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

PROTOTYPES:

void getPackage(char);
bool validPackage(char);
void getHours(int);
bool validHours(int);
bool notValidHours(double);
double calculatePkg_A(int);
double calculatePkg_B(int);
double calculatePkg_C(int);

const double MIN_HOURS = 0.0;
const double MAX_HOURS = 720.0;

FUNCTIONS:

int main()
{
double hours;
char ch;

getPackage(ch);
getHours(hours);



cout << "Test Case\tPackage\t\tHours" << endl;
cout << " 1\t\t a\t\t" << left << calculatePkg_A(hours) << endl;
cout << " 2\t\t b\t\t" << left << calculatePkg_B(hours) << endl;
cout << " 3\t\t c\t\t" << left << calculatePkg_C(hours) << endl;
return 0;
}

//Letting the user choose their preferred package.

void getPackage(char choice)
{
cout << "Select a package: \n";
cout << "Package A: $15 per month w/ 50 hours of access. (Additional hours are $2.00 per hour)\n";
cout << "Package B: $20 per month w/ 100 hours of access. (Additional hours are $1.50 per hour)\n";
cout << "Package C: $25 per month w/ 150 hours of access. (Additional hours are $1.00 per hour)\n";
cin >> choice;
}

//When the user chooses a package, the do-while loop will choose the calculation function according to the package.

bool validPackage(char choice)
{
return (choice >= 'A' && choice <= 'C')|| (choice >= 'a' && choice <= 'c');
}

//Bool Function to validate hours are within range.

bool validHours (int hours)
{
return (hours >= MIN_HOURS && hours <= MAX_HOURS);
}

//Bool Function of hours outside of range.

bool notValidHours (double hours)
{
return (hours < MIN_HOURS || hours > MAX_HOURS );
}


//Function to get hours within range.

void getHours(int hours)
{
do
{
cout << "Enter hours: \n";
cin >> hours;
} while ( notValidHours(hours));
}

//Calculation function of Package A.

double calculatePkg_A(int hours)
{
double usage(0);
double rate = 15;

if ( hours <= 50 )
{
usage = rate * hours;
}
else
{
usage = rate * 50 + 2.0 * rate * (hours - 50);
}
return usage;
}


//Calculation function of Package B.

double calculatePkg_B(int hours)
{
double usage(0);
double rate = 20;

if ( hours <= 100 )
{
usage = rate * hours;
}
else
{
usage = rate * 100 + 1.5 * rate * (hours - 100);
}
return usage;
}


//Calculation function of Package C.

double calculatePkg_C(int hours)
{
double usage(0);
double rate = 25;

if ( hours <= 150 )
{
usage = rate * hours;
}
else
{
usage = rate * 150 + 1.0 * rate * (hours - 150);
}
return usage;
}

OUTPUT:

Test Case Package Hours
1 a 0
2 b 0
3 c 0
Last edited on
agirideep,
PLEASE USE CODE TAGS (the <> formatting button to the right), when posting code!

Along with the proper indenting, it makes it easier to read your code, and thus also easier to respond to your post.

Tutorials on how to use code tags:

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/


I've found the second link to be the most help.

Hint: You can hit "edit post", highlight your code and then press the <> formatting button.
Note: This will not automatically indent your code! That part is up to you!

I've found it's easiest to copy and paste pre-indented code directly from the IDE, that way it is already properly formatted.

You can use the "preview" button at the bottom to see how it looks.

Using Code Tags, @Handy Andy, from cplusplus.com

Additionally, you should probably comment out your labels (PROTOTYPES, FUNCTIONS, etc), just in case.

With regard to your problem though, try running it in the debugger and check your variables. That should at least help you figure out where the problem is.

Hope this helps!
max
Last edited on
There' issues with parameter passing and mix of int/double. Perhaps (not properly tested):

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

void getPackage(char&);
bool validPackage(char);
void getHours(double&);
bool validHours(double);
bool notValidHours(double);
double calculatePkg_A(double);
double calculatePkg_B(double);
double calculatePkg_C(double);

const double MIN_HOURS {};
const double MAX_HOURS {720.0};

int main()
{
	double hours {};
	char ch {};

	getPackage(ch);
	getHours(hours);

	cout << "Test Case\tPackage\t\tHours" << endl;
	cout << " 1\t\t a\t\t" << left << calculatePkg_A(hours) << endl;
	cout << " 2\t\t b\t\t" << left << calculatePkg_B(hours) << endl;
	cout << " 3\t\t c\t\t" << left << calculatePkg_C(hours) << endl;
}

void getPackage(char& choice)
{
	cout << "Select a package: \n";
	cout << "Package A: $15 per month w/ 50 hours of access. (Additional hours are $2.00 per hour)\n";
	cout << "Package B: $20 per month w/ 100 hours of access. (Additional hours are $1.50 per hour)\n";
	cout << "Package C: $25 per month w/ 150 hours of access. (Additional hours are $1.00 per hour)\n";
	cin >> choice;
}

bool validPackage(char choice)
{
	return (choice >= 'A' && choice <= 'C') || (choice >= 'a' && choice <= 'c');
}

bool validHours(double hours)
{
	return (hours >= MIN_HOURS && hours <= MAX_HOURS);
}

bool notValidHours(double hours)
{
	return (hours < MIN_HOURS || hours > MAX_HOURS);
}

void getHours(double& hours)
{
	do {
		cout << "Enter hours: \n";
		cin >> hours;
	} while (notValidHours(hours));
}

double calculatePkg_A(double hours)
{
	const double rate {15};

	return hours <= 50 ? rate * hours : rate * 50 + 2.0 * rate * (hours - 50);
}

double calculatePkg_B(double hours)
{
	const double rate {20};

	return hours <= 100	? rate * hours : rate * 100 + 1.5 * rate * (hours - 100);
}

double calculatePkg_C(double hours)
{
	const double rate {25};

	return hours <= 150 ? rate * hours : rate * 150 + 1.0 * rate * (hours - 150);
}


Last edited on
Hello agirideep,

In seeplus's code you may not spot the differences right off.

In "main" you have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    double hours{};  // <--- ALWAYS initialize all your variables.
    char ch{};

    getPackage(ch);
    getHours(hours);

    cout << 
        "Test Case\tPackage\t\tHours\n"
        " 1\t\t a\t\t" << /*left <<*/ calculatePkg_A(hours) << "\n"
        " 2\t\t b\t\t" << /*left <<*/ calculatePkg_B(hours) << "\n"
        " 3\t\t c\t\t" << /*left <<*/ calculatePkg_C(hours) << "\n";

    return 0;
}


Your function is:
1
2
3
4
5
6
7
8
9
10
void getHours(int hours)
{
    do
    {
        cout << "Enter hours: ";
        cin >> hours;
    } while (notValidHours(hours));

    std::cout << '\n'; // <--- Used for testing. Comment or remove when finished.
}

Is changing the type of "hours" intentional?

Line 1 defines the variable "hours" and line 10 destroys that variable when the function looses scope. Passing "hours" by value does not change the value of "hours" back in "main". You would need to return hours when the do/while loop ends or pass by reference.

Something to consider here is checking for input that is a non numeric value.

Just a note: in"main" is an example of not needing a "cout" and "endl" for every line. Also the use of the insertion operator(<<) will chain everything together.

"std::left" and std::right" are only useful when using "std::setw()" to position the output to either the left or right side of the block defined by "std::setw()". As you have used it here it has no effect on the output.

And in:
1
2
3
4
5
6
7
8
9
10
11
12
void getPackage(char choice)
{
    cout <<
        "Select a package: \n"
        "Package A: $15 per month w/ 50 hours of access. (Additional hours are $2.00 per hour)\n"
        "Package B: $20 per month w/ 100 hours of access. (Additional hours are $1.50 per hour)\n"
        "Package C: $25 per month w/ 150 hours of access. (Additional hours are $1.00 per hour)\n"
        " Enter choice: ";  // <--- Added.
    cin >> choice;

    std::cout << '\n';  // <--- Added.
}

You can see how easy it is to add something as I did with line 8

Here the quoted strings become 1 large string even though they are on separate lines. The compiles does not care about the white space.

"getPackage" also has the same problem as "getHours" you send "ch" to the function, but its value is lost when the function looses scope. Not really a problem as you do not use the input anywhere else in the program, but you should in the functions that calculate.

The program is OK for testing, but need reworked for a proper use. Maybe just 1 calculate function the has parameters for hours and the choice of the plan.

If this is a school assignment then please post the instructions that you were given so everyone will know what you have to do and not make suggestions soly based on your code which may not be the correct approach.

I would suggest deciding on if "hours" should be a double or an "int" and make it consistent through out the program.

Andy
Hello agirideep,

I do have some suggestions, but am stuck not knowing what you are trying to accomplish with the program.

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
#include <iostream>
#include <iomanip>
#include <cctype>  // <--- For "std::tolower() and std::toupper()" + others.
#include <cmath>

using namespace std;  // <--- Best not to use.

//  Constant variables:
const double MIN_HOURS{};
const double MAX_HOURS{ 720.0 };
constexpr double
    PLAN_A_BASE_COST{ 15.0 },
    PLAN_B_BASE_COST{ 20.0 },
    PLAN_C_BASE_COST{ 25.0 };
constexpr double
    PLAN_A_BASE_HOURS{ 50.0 },
    PLAN_B_BASE_HOURS{ 100.0 },
    PLAN_C_BASE_HOURS{ 150.0 };
constexpr double
    PLAN_A_Add_HOURS{ 2.0 },
    PLAN_B_Add_HOURS{ 1.5 },
    PLAN_C_Add_HOURS{ 1.0 };

//  PROTOTYPES:
void getPackage(char& choice);
bool validPackage(char choice);
void getHours(double& hours);
bool validHours(double hours);
bool notValidHours(double hours);
double calculatePkg_A(const double hours, const char choice);
double calculatePkg_B(const double hours);
double calculatePkg_C(const double hours);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getPackage(char& choice)
{
    cout <<
        "Select a package: \n"
        "Package A: $15 per month w/ 50 hours of access. (Additional hours are $2.00 per hour)\n"
        "Package B: $20 per month w/ 100 hours of access. (Additional hours are $1.50 per hour)\n"
        "Package C: $25 per month w/ 150 hours of access. (Additional hours are $1.00 per hour)\n"
        " Enter choice: ";  // <--- Added.
    cin >> choice;

    choice = static_cast<char>(std::toupper(static_cast<unsigned char>(choice)));
    
    std::cout << '\n';  // <--- Added.
}


1
2
3
4
5
bool notValidHours(double hours)
{
    //return (hours <= MIN_HOURS || hours > MAX_HOURS);
    return std::cout << "\n     Invalid hours! Try again\n\n", (hours < MIN_HOURS || hours > MAX_HOURS);
}


I used this 1 as an example because I am not sure what I want to do with "A":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double calculatePkg_B(const double hours)
{
    double usage{ PLAN_B_BASE_COST };
    //double rate = 20;

    if (hours <= 100)
    {
        /*usage = rate * hours*/;
    }
    else
    {
        usage += (std::ceil(hours) - PLAN_B_BASE_HOURS) * PLAN_B_Add_HOURS;
    }

    return usage;
}

The idea here is that "usage" starts with a base cost then you would add to that any additional hours. Ih "hours" has a decimal value you need to round that up to the next hour before any calculation can be done.

When I run the program I get this:

Select a package:
Package A: $15 per month w/ 50 hours of access. (Additional hours are $2.00 per hour)
Package B: $20 per month w/ 100 hours of access. (Additional hours are $1.50 per hour)
Package C: $25 per month w/ 150 hours of access. (Additional hours are $1.00 per hour)
 Enter choice: a

Enter hours: 120

Test Case    Package    Hours    Cost
 1              A       120.00   $155.00
 2              B       120.00   $50.00
 3              C       120.00   $3000.00 This 1 not changed.


I also noticed the function "notValidHours" still needs some work.

Andy

Edit:
Last edited on
Registered users can post here. Sign in or register to post.