Hello Battlefire,
Is there any way we can define the variables locally? One of the rules of my assignment is that I can't use global variables.
|
Yes. and for the second part that is a good thing.
1 2 3 4 5 6
|
int main()
{
bool senior{ 1 };
int months{ 12 }, personal{ 6 };
std::cout << std::fixed << std::showpoint << std::setprecision(2);
|
These variables are local to the "main" function.
As your prototype states:
void getInfo(bool &senior, int &months, int &personal);
. These variables become local to the function, but since they are passed by reference they need something to reference back where the function was called from.
When you look at the prototype and the function call they match as they should, but the function call also need to match, i.e., sending the correct variables to the function.
Global variables may appear to be easy and solve a problem, but at some point in the future there will be a function that will change a global variable and you will spend hours or days trying to find it. Chiefly for this reason it is best to avoid using global variables. Define them in "main" and pass them to the functions that need them or define the variables in the functions that would use them. Either choice would depend on the variable and where it was needed.
Also, for this:
1 2 3
|
constexpr double MEMBERSHIP{ 50.0 };
constexpr double TWENTYPER_DISC{ 0.8 }; // <--- Twenty percent discount.
constexpr double THIRTYPER_DISC{ 0.7 }; // <--- Thirty percent discount.
|
|
First these variable are acceptable as global variables because they can not be changed by the program. If you do try to change them the compiler will complain.
Given a line of code like:
months = months * 50.00;
the "50.00" is considered a magic number and should be avoided. In this program you only use the value four times, but consider a program that is 600 lines of code, yes you will likely do this before you learn a better way, and in the 600 lines you have 20 places where you use "50.00". Should you have to change the "50.00" you would have to go through your code and change everyone. Are you sure that you can find every ocurance the first time?
The constant global variable means that you only have one place to make a change and it will be seen everywhere you use that variable name.
You could argue the point that since these are defined as constant variables and can not be changed they are not like a regular variable.
It is not that your calculations are wrong because of not using the constants. They are wrong because of what you are doing.
I assumed that calcCost(senior, months, personal); that is in getInfo() would be a pass by reference for the next function which is calcCost(bool senior, int months, int personal) . How would you go about transferring the values in those variables from getInfo to calcCost? |
You did make that function call correctly, but this should not be done in the "getInfo" function.
When you define the variables "senior", "months" and "personal" in main you can use them to call the function "calcCost". Actually since "calcCost" returns a value you can put the function call in the "cout" statement and use the return value for the output.
In the function "calcCost" "months" arrives to the function with a value of 12. In the first if statement
1 2 3 4 5 6 7 8 9
|
if(months >= 12)
{
months = months * 50.00;
months = months * (1 - 0.20); //Gets the discount of %20
}
else
{
months = months * 50.00; //If no discount just do operations of $50 per month
}
|
Which would be:
1 2 3 4 5 6 7 8 9
|
if(months >= 12)
{
months = 12 * 50.00;
months = 600 * (1 - 0.20); //Gets the discount of %20. The same as * 0.8
}
else
{
months = 12 * 50.00; //If no discount just do operations of $50 per month
}
|
When this is finished "months" would have the value of 480.
So when you get to the next time you need "months":
1 2 3 4 5 6 7 8 9 10 11
|
if (senior == true)
{
if (months >= 12)
{
months = months * 50.00;
months = months * (1 - 0.20); //Gets the discount of %20
}
else
{
months = months * 50.00; //If no discount just do operations of $50 per month
}
|
It would look like:
1 2 3 4 5 6 7 8 9 10 11
|
if (senior == true)
{
if (480 >= 12)
{
months = 480 * 50.00;
months = 24000 * (1 - 0.20); //Gets the discount of %20
}
else
{
months = 480 * 50.00; //If no discount just do operations of $50 per month
}
|
As you can see trying to use and reuse the variable "months" and "personal" has a very different outcome than what you may have been thinking.
You need something else to keep track of the total amount that you are adding up.
Hope that helps,
Andy