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