I need help with functions with an assignment

Pages: 12
I'm having trouble with getting my functions right in a assignment. I need some help to get my code correct. You can see the cout's in void displayInfo() on what I'm trying to accomplish.

I have to use these function prototypes. But I can add more.
1
2
void getInfo(bool &senior, int &months, int &personal);
double calcCost(bool senior, int months, int personal);


I also cant use global variables.

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>
#include <iomanip>

using namespace std;

    void displayInfo();
    void getInfo(bool &senior, int &months, int &personal);
    double calcCost(bool senior, int months, int personal);

int main()
{
    
    displayInfo();
    
    getInfo();

    calcCost();
    
    return 0;
}

void displayInfo()
{
    cout << "Welcome to out Fitness Center!" << endl;
    cout << "One month membership is $50.00 and personal training is $30.00 for every session." << endl;
    cout << "If you are a senior citizen, the discount is 30%" << endl;
    cout << "If you membership you paid is 12 or more months, the discount is 20%" << endl;
    cout << "If more than five personal training sessions are bought, the  discount is 20%" << endl;
}

void getInfo(bool &senior, int &months, int &personal)
{
	cout << "Please enter how many months: " << endl;
	cin >> months;
	cout << "Are you an senior? (Type 1 for yes or 0 for no): " << endl;
	cin >> senior;
	cout << "How many personal training sessions?: " << endl;
	cin >> personal;

    calcCost(senior, months, personal); //Pass by reference to calcCost function
    
}

double calcCost(bool senior, int months, int personal)
{
    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
    }
    if(personal > 5)
    {
        personal = personal * 30.00;
        personal = personal * (1 - 0.20); //Gets the discount of %20
    }
    else
    {
        personal = personal * 30.00; //If no discount just do operations of $30 per personal training session
    }
    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
        }
        if(personal > 5)
        {
            personal = personal * 30.00;
            personal = personal * (1 - 0.20); //Gets the discount of %20
        }
        else
        {
            personal = personal * 30.00; //If no discount just do operations of $30 per personal training session
        }
    
        months = months + personal * (1 - 0.30); // Having everything of total cost be out into months
        
        }
    
    
    months = months + personal;

    cout << "The total cost is: " << months << endl;
    return months;
}
Last edited on
Hello Battlefire,

As a start your prototype and function say: void getInfo(bool &senior, int &months, int &personal). You define three variables by reference, but where are they defined? Then in "main" you call the function with no parameters. The error messages from the compiler should have told something was wrong. Even your should have told you something was wrong.

The prototype and function definition need to match, which they do then the function call needs to send the correct variables to the function, hint hint, nudge nudge.

The same would be true about the "calcCost" function and call.

I will give the program a better check and let you know if I find anything else.

Hope that helps,

Andy
@Handy Andy

I appreciate your help again.

What do you mean by them not being defined? Aren’t they defined when the user inputs the three variables which are part of the function?

And how would I go about with the parameters?
Last edited on
Look at your function prototypes and your function calls. Does your function calls all have the same number and types of variables as described in the function prototype?

Hello Battlefire,

You have defined the variables in the parameters of the function, i.e., calcCost(bool senior, int months, int personal), but these variables are local to the function. And in the "getInfo"function those variables are a reference to a variable that should be defined elsewhere, i.e., back in "main" where the function is called.

In working with the program here are some things that might help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

constexpr double MEMBERSHIP{ 50.0 };
constexpr double TWENTYPER_DISC{ 0.8 }; // <--- Twenty percent discount.
constexpr double THIRTYPER_DISC{ 0.7 }; // <--- Thirty percent discount.

void displayInfo();
void getInfo(bool &senior, int &months, int &personal);
double calcCost(bool senior, int months, int personal);

int main()
{
	bool senior{ 1 };  // <--- Define variables here. These are set up for testing.
	int months{ 13 }, personal{ 6 };

	std::cout << std::fixed << std::showpoint << std::setprecision(2);

Line 20 only needs to be done once and will affect all "std::cout" statements until it is changed elsewhere in the program.

I rearranged the "displayInfo" function this way:
1
2
3
4
5
6
7
8
void displayInfo()
{
	std::cout << "\n Welcome to out Fitness Center!\n";
	std::cout << "\n One month membership is $50.00 and personal training is $30.00 for every session.";
	std::cout << "\n If you are a senior citizen, the discount is 30%";
	std::cout << "\n If you membership you paid is 12 or more months, the discount is 20%";
	std::cout << "\n If more than five personal training sessions are bought, the  discount is 20%\n" << std::endl;
}

You only need one "std::endl" on the last line. The other lines you can make use of the new line (\n).

In the "getInfo" function the function should do only one thing. Putting the function call to "calcCost" is two things. The problem is that you have the function call correct, but this line should be in "main".

I changed the prompts:
1
2
std::cout << " Please enter how many months: ";
std::cin >> months;

By removing the "std::endl;" from the end of the prompt this puts the "std::cin" on the same line. IMHO I feel this works better, but you are free to do what you want.

For the "calcCost" function for the most part you have the right idea, but going about it the wrong way. You said you have to use:
double calcCost(bool senior, int months, int personal); This is fine, but where does it say that you can only use these variables in the function?

To give you an idea:
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
double calcCost(bool senior, int months, int personal)
{
	double totalCost{}, totalPersonal{}, totalSenior{};

	if (months >= 12)
	{
		totalCost = months * 50.00;
		//months = months * (1 - 0.20); //Gets the discount of %20
		//totalCost += totalCost - (totalCost * TWENTYPER_DISC);
		totalCost *= TWENTYPER_DISC;
	}
	else
	{
		totalCost = months * 50.00; //If no discount just do operations of $50 per month
	}

	if (personal > 5)
	{
		totalPersonal += personal * 30.00;
		totalCost += totalPersonal * TWENTYPER_DISC; //Gets the discount of %20
	}
	else
	{
		totalCost += personal * 30.00; //If no discount just do operations of $30 per personal training session
	}

This is as far as I managed to get to because of questions I have.

Is it your intention to give an extra 20% discount on the number of months and the sessions with a personal trainer or should it be 20% of the total cost?

That should give you something to work on for awhile.

Hope that helps,

Andy
@Handy Andy

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. So how would I define these variables under this rule? I thought that the variables were already defined because the variable types are there and within the parameter of the function. I'll contact my professor on this "no global variable" rule because it is pretty tedious without them.

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. 


Why create constexpr type variables for the individual discounts? Is this because my calculations are wrong without them?

Also regarding this:
For the "calcCost" function for the most part you have the right idea, but going about it the wrong way. You said you have to use:
double calcCost(bool senior, int months, int personal); This is fine, but where does it say that you can only use these variables in the function?


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?

Is it your intention to give an extra 20% discount on the number of months and the sessions with a personal trainer or should it be 20% of the total cost?


The 20% discount is applies to both the months and the personal separately. So if the user inputs 12 for months and 5 for personal. The total discount would be 40% based on the receipt.


Last edited on
Is there any way we can define the variables locally?

Yes, of course. Do you not know how to create a local variable? That's one of the very first things any C++ course will cover.

I thought that the variables were already defined because the variable types are there and within the parameter of the function.

When you define the parameters of the function, you're telling the compiler what entities you're going to pass into your function.

However, you still need to actually create those entities that you're going to pass in. In main(), you need to actually have a bool and two ints to pass into getInfo().

I'll contact my professor on this "no global variable" rule because it is pretty tedious without them.

I hate to break the bad news to you, but global variables are usually a bad idea. It's a very, very good thing that your professor is forbidding you to use them, because you should learn how to create things where they're needed, and to pass them around properly. It's good that your professor is making you get into the habit of doing that early.

Why create constexpr type variables for the individual discounts? Is this because my calculations are wrong without them?

Using constants with informative names is often better than using magic numbers. It makes the code more readable, and makes it easier to modify the values if you need to.

In this case, it's probably overkill. Nobody should need to have it explained that a 20% discount means you have 80% of the cost, or that you get that by multiplying by .8, but it's still a good practise to get into the habit of.

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 are correctly passing values from getInfo() to calcCost(). You're not passing them by reference, you're passing them by value, because the parameters for calcCost() aren't defined as references, but that's not a problem with the code, just with your explanation of it.

The problem is that you're not passing anything from main() to getInfo().

Also, I don't understand why you're calling calcCost() from main(). You're already calling calcCost() from getInfo(); why do you need to call it a second time?
Last edited on
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
Is this the right direction?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    
    bool senior;
    int months;
    int personal;

    displayInfo();
    
    getInfo();
    calcCost(bool senior, int months, int personal);

    cout << months << endl;
    
    return 0;
}
Last edited on
Hello Battlefire,

Sorry for the delay. I was having some new problems with the computer yesterday.

Yes and No.

You have defined your variables, but it would be a good idea to initialize them. Sometimes the compiler will complain about using an uninitialized variable when sending it to a function.

Line 11 returns a value that you never collect. Also you only need the variable names for the function parameters. Otherwise it makes a good prototype.

Line 13 will print the value of months that was entered with the call to "getInfo", which BTW is missing its parameters.

To give you an idea this is what I did:
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
#include <iostream>
#include <iomanip>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

constexpr double MEMBERSHIP{ 50.0 };
constexpr double TWENTYPER_DISC{ 0.8 }; // <--- Twenty percent discount.
constexpr double THIRTYPER_DISC{ 0.7 }; // <--- Thirty percent discount.

void displayInfo();
void getInfo(bool &senior, int &months, int &personal);
double calcCost(bool senior, int months, int personal);

int main()
{
	bool senior{ 1 };
	int months{ 12 }, personal{ 6 };

	std::cout << std::fixed << std::showpoint << std::setprecision(2);

	displayInfo();

	//getInfo(senior, months, personal);

	std::cout << "\n The total cost is: $" << calcCost(senior, months, personal) << std::endl;

	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;	
}

In line 26 the call to the function will use the return value of the function in the "cout" statement.

Hope that helps,

Andy

P.S.

By initializing the variables this way and commenting out the call to "getInfo" you do not have to enter the same information each time the program runs. When you are finished working just leave empty {}s on the variables and uncomment the call to "getInfo".
Last edited on
Ok, I'll continue to work on my main and let you know if I face any more difficulties.
You mention about my calculations for calcCost() and that I need to track the total cost. Now that I'm looking at it. My algorithm is very flawed in its math. I'm using months as the base for my total calculation. But I'm re-using months for senior which will screw up my total. Do I need another function for that? Because I can't initialized another variable for the given functions according to my professor.

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
double calcCost(bool senior, int months, int personal)
{
    if(months >= 12)
    {
        months = months * 50.00;
        months = months * (1 - 0.20); 
    }
    else
    {
        months = months * 50.00; 
    }
    if(personal > 5)
    {
        personal = personal * 30.00;
        personal = personal * (1 - 0.20); 
    }
    else
    {
        personal = personal * 30.00; 
    }
    if(senior == true)
    {
        months = months * 50.00;
        senior = senior * 30.00;
    }    
    
    
    months = months + personal;

    cout << "The total cost is: " << months << endl;
    return months;
}


I know giving I have to get rid of the magic numbers and replace them with constexpr type variables. But that won't help with how I need a separate variable for total cost. So I need another function, correct?

Something like:
double TotalCalc(bool senior, int months, int personal, int totalCost);
Last edited on
Hello Battlefire,

You have to use double calcCost(bool senior, int months, int personal) and that works.What you need is:
1
2
3
double calcCost(bool senior, int months, int personal)
{
    double totalCost{}, totalPersonal{}, totalSenior{};

I used this and it made a big difference.

You can not change the prototype or function definition, but at the same time you can not use those variables and make changes to them without becoming a problem. As you have realized.

Unless you were given the body of the function and told that you could not change it you had to write your own code. This means that inside the {}s of the function you have to use what you need.

I had a thought while working on ht function. Write the prototype and function definition this way:
1
2
3
4
5
6
7
8
9
10
// prototype.

//double calcCost(bool senior, int months, int personal);
double calcCost(const bool senior, const int months, const int personal);

// Function definition.

//double calcCost(bool senior, int months, int personal)
double calcCost(const bool senior, const int months, const int personal)
{  // <--- opening brace of function. 

This way you will not be able to change the values of these variables. When you are done with the function uncomment the first line and delete the second line. Also the comment on line 10 can be removed.

This will force you to use other variables defined in the function.

Hope that helps,

Andy
@Handy Andy

I'm not sure we I'm allowed to change the int/bool types to const int/bool. And based of what I put together for calcCost(bool senior, int months, int personal). I wouldn't need them because I'm not changing their values due to having additional variables for the totals.

Here is what I got put together for that function.

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
//constexpr of for all the variables for monthly/personal costs and all discount types. This should get rid of the magic numbers.
constexpr double monthsPay = 50.00;
constexpr double monthsPayDisc = 0.20;
constexpr double personalPay = 30.00;
constexpr double personalPayDisc = 0.20;
constexpr double seniorPayDisc = 0.30;


double calcCost(bool senior, int months, int personal)
{
	double totalCost{}, totalMonths{}, totalPersonal{}, totalSenior{};
	
	if (months >= 12)
	{
		totalMonths = months * monthsPay;
		totalMonths = totalMonths * (1 - monthsPayDisc); //Gets the discount of 20%
	}
	else
	{
		totalMonths = months * monthsPay; //If no discount just do operations of $50 per month
	}
	if (personal > 5)
	{
		totalPersonal = personal * personalPay;
		totalPersonal = totalPersonal * (1 - personalPayDisc); //Gets the discount of 20%
	}
	else
	{
		totalPersonal = totalPersonal * personalPay; //If no discount just do operations of $30 per personal training session
	}
	
	totalCost = totalMonths + totalPersonal;
	
	if (senior == true)
	{
		totalCost = totalCost * seniorPayDisc;
	}

	return totalCost;
}


I have a question, can I put the constexpr in the calcCost() function? Or are they only tied to being global variables?
Last edited on
Hello Battlefire,

I think you misunderstood what I was saying. In the function definition using "const" is not meat to be permanent just temporary so that you would not be able to change the value of those variables in the function. The "const" is to be removed after the function is working correctly.

In the line: double totalCost{}, totalMonths{}, totalPersonal{}, totalSenior{}; I see no use for "totalMonths". The name is misleading.

I am not going to gurentee that this is 100% correct, but I believe that it is what you are looking for.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
double totalCost{}, totalPersonal{}, totalSenior{};

if (months >= 12)
{
	totalCost = months * 50.00;
	totalCost *= TWENTYPER_DISC;
}
else
{
	totalCost = months * 50.00; //If no discount just do operations of $50 per month
}

if (personal > 5)
{
	totalPersonal = personal * 30.00;
	totalCost += totalPersonal * TWENTYPER_DISC; //Gets the discount of %20
}
else
{
	totalCost += personal * 30.00; //If no discount just do operations of $30 per personal training session
}


I defined TWENTYPER_DISC as "0.8" which is the same as "1 - 0.2". I just saved a step this way.

Line 36 can be shortened to totalCost *= seniorPayDisc;. It is the same as what you did.

For if (senior == true). What is in the () is the same as if (true == true) or if (1== 1). As a bool variable it can have only two values; (0) zero meaning "false" or (1) one meaning "true". Using the names "false" and "true" just makes it easier to read. So in the end with "senior" as a bool variable all you need is if (senior) comparing it to "true" is redundant.

Other than not using "totalCost" properly everything else look OK for now. I will give it a test shortly.

Hope that helps,

Andy
Hello Battlefire,

Sorry I managed to hit send when I wanted preview.

I have a question, can I put the constexpr in the calcCost() function? Or are they only tied to being global variables?

Yes you can and No it is not just for global variables. OK, so I do not fully understand everything here https://en.cppreference.com/w/cpp/language/constexpr , but I m trying.

So far I have found it works well for numerical types, but I have seen it used with a "std::string", but I have not had much luck with this so far.

Since "constexpr" is available form C++11 on I am working on learning and using the C++11 standards. That is why I use it.

Hope that helps,

Andy
Ok so here is what I have.

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
#include <iostream>
#include <iomanip>

using namespace std;

void displayInfo();
void getInfo(bool &senior, int &months, int &personal);
double calcCost(bool senior, int months, int personal);

int main()
{
	
	int totalCost;
	
	displayInfo();
	
	double calcCost(bool senior, int months, int personal);
	
	//getInfo(bool senior, int months, int personal);

	cout << totalCost << endl;
	
	//bool senior{  }; 
	//int months{  }, personal{  };
    //std::cout << std::fixed << std::showpoint << std::setprecision(2);

	return 0;
}

void displayInfo()
{
	cout << "Welcome to out Fitness Center!" << endl;
	cout << "One month membership is $50.00 and personal training is $30.00 for every session." << endl;
	cout << "If you are a senior citizen, the discount is 30%" << endl;
	cout << "If you membership you paid is 12 or more months, the discount is 20%" << endl;
	cout << "If more than five personal training sessions are bought, the  discount is 20%" << endl;
}

void getInfo(bool &senior, int &months, int &personal)
{
	cout << "Please enter how many months: " << endl;
	cin >> months;
	cout << "Are you an senior? (Type 1 for yes or 0 for no): " << endl;
	cin >> senior;
	cout << "How many personal training sessions?: " << endl;
	cin >> personal;
}

double calcCost(bool senior, int months, int personal)
{
	constexpr double monthsPay = 50.00;
    constexpr double monthsPayDisc = 0.20;
    constexpr double personalPay = 30.00;
    constexpr double personalPayDisc = 0.20;
    constexpr double seniorPayDisc = 0.30;
	
	double totalCost{}, totalMonths{}, totalPersonal{}, totalSenior{};
	
	if (months >= 12)
	{
		totalMonths = months * monthsPay;
		totalMonths = totalMonths * (1 - monthsPayDisc); //Gets the discount of 20%
	}
	else
	{
		totalMonths = months * monthsPay; //If no discount just do operations of $50 per month
	}
	if (personal > 5)
	{
		totalPersonal = personal * personalPay;
		totalPersonal = totalPersonal * (1 - personalPayDisc); //Gets the discount of 20%
	}
	else
	{
		totalPersonal = totalPersonal * personalPay; //If no discount just do operations of $30 per personal training session
	}
	
	totalCost = totalMonths + totalPersonal;
	
	if (senior == true)
	{
		totalCost = totalCost * seniorPayDisc;
	}

	return totalCost;
}


There aren't any errors but it goes through the program an completely skips void getInfo(bool &senior, int &months, int &personal) and calcCost(bool senior, int months, int personal) with a output of 0.

Also my professor stated I have to keep using namespace std; because he has something for us to do later one with our code.
Hello Battlefire,
There aren't any errors but it goes through the program an completely skips void getInfo(bool &senior, int &months, int &personal)


Of course it does. Did you wonder why there is a comment on line 19? There may be no errors, but I did get a warning that int totalCost; in "main" is uninitialized.

and calcCost(bool senior, int months, int personal) with a output of 0.

Of course it does did you expect something different? Line 17 is a very nice prototype just like line 8. Is there a reason why you feel that you need another prototype instead of a function call?

Lines 23 - 25 should be at the top of "main" where they are needed not commented out after they are used.

I hate to say this, it looks like you went from a program that was just about finished to one that you tried to start over and failed miserably on.

Also my professor stated I have to keep using namespace std; because he has something for us to do later one with our code.
That is alright. I realize you paid the money to be taught the wrong way or in the more difficult way.

Even when you fix line 17 to be a function call you are sending variables that do not exist. If it does not give you errors the results will be undefined or something that you do not want.

I also noticed the lines 17 and 19 should be reversed.

Andy
Handy Andy wrote:
I hate to say this, it looks like you went from a program that was just about finished to one that you tried to start over and failed miserably on.

As someone whose posts are usually full of errors, and who frequently posts outright nonsense and lies about the language, you're in no position to be this rude about anyone else's code. You owe the OP an apology.
Last edited on
@MikeyBoy,

It isn't him it is me. I'm too slow to grasp things so it is understandable that he lost patience with me.

@Handy Andy,

Is this more like it?

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
#include <iostream>
#include <iomanip>

using namespace std;

void displayInfo();
void getInfo(bool senior, int months, int personal);
double calcCost(bool senior, int months, int personal);

    //bool senior{  }; 
	//int months{  }, personal{  };
    //std::cout << std::fixed << std::showpoint << std::setprecision(2);

int main()
{
	
	bool senior;
	int months;
	int personal;
	double Cost;
	
	displayInfo();
	
	getInfo(senior,months,personal);
	calcCost(senior, months, personal);
	
	Cost = calcCost(senior, months, personal);
	cout << Cost << endl;

	return 0;
}

void displayInfo()
{
	cout << "Welcome to out Fitness Center!" << endl;
	cout << "One month membership is $50.00 and personal training is $30.00 for every session." << endl;
	cout << "If you are a senior citizen, the discount is 30%" << endl;
	cout << "If you membership you paid is 12 or more months, the discount is 20%" << endl;
	cout << "If more than five personal training sessions are bought, the  discount is 20%" << endl;
}

void getInfo(bool &senior, int &months, int &personal)
{
	cout << "Please enter how many months: " << endl;
	cin >> months;
	cout << "Are you an senior? (Type 1 for yes or 0 for no): " << endl;
	cin >> senior;
	cout << "How many personal training sessions?: " << endl;
	cin >> personal;
}

double calcCost(bool senior, int months, int personal)
{
	constexpr double monthsPay = 50.00;
    constexpr double monthsPayDisc = 0.20;
    constexpr double personalPay = 30.00;
    constexpr double personalPayDisc = 0.20;
    constexpr double seniorPayDisc = 0.30;
	
	double totalCost{}, totalMonths{}, totalPersonal{}, totalSenior{};
	
	if (months >= 12)
	{
		totalMonths = months * monthsPay;
		totalMonths = totalMonths * (1 - monthsPayDisc); //Gets the discount of 20%
	}
	else
	{
		totalMonths = months * monthsPay; //If no discount just do operations of $50 per month
	}
	if (personal > 5)
	{
		totalPersonal = personal * personalPay;
		totalPersonal = totalPersonal * (1 - personalPayDisc); //Gets the discount of 20%
	}
	else
	{
		totalPersonal = totalPersonal * personalPay; //If no discount just do operations of $30 per personal training session
	}
	
	totalCost = totalMonths + totalPersonal;
	
	if (senior == true)
	{
		totalCost = totalCost * seniorPayDisc;
	}

	return totalCost;
}


I had to define senior, months, and personal locally again because it would give a undefined error for them.

I decided to call double calcCost(bool senior, int months, int personal) to a local variable int Cost based from this: https://www.tutorialspoint.com/cplusplus/cpp_functions.htm

Also, if you lost patience with me because of my slowness to grasp things. It is fine and we can stop it here. I can sort everything out myself from this point on.
Line 85, you are calculating the senior discount wrong. Look how you are calculating the other two discounts for why the senior discount has a logic error.

Your code is for the most part quite sound. I made some modifications. Such as changing how the discounts (if any) are calculated. A 20% discount becomes 80% of the regular price, etc.

I also added some output statements (removable) to indicate if the patron gets any discounts or not, and what the running bill is.

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
#include <iostream>

void displayInfo();
void getInfo(bool&, int&, int&);  // have to add the references!
double calcCost(bool senior, int months, int personal);

int main()
{
   bool senior { };
   int months { };
   int personal { };

   displayInfo();

   getInfo(senior, months, personal);

   double Cost = calcCost(senior, months, personal);

   std::cout << "\nThe total cost is $" << Cost << '\n';
}

void displayInfo()
{
   std::cout << "Welcome to our Fitness Center!\n"
             << "One month membership is $50.00 and personal training is $30.00 per session.\n"
             << "If you are a senior citizen, the discount is 30%\n"
             << "If you membership you paid is 12 or more months, the discount is 20%\n"
             << "If more than five personal training sessions are bought, the  discount is 20%\n\n";
}

void getInfo(bool& senior, int& months, int& personal)
{
   std::cout << "Please enter how many months: ";
   std::cin >> months;

   std::cout << "Are you an senior? (Type 1 for yes or 0 for no): ";
   std::cin >> senior;

   std::cout << "How many personal training sessions?: ";
   std::cin >> personal;

   std::cout << '\n';
}

double calcCost(bool senior, int months, int personal)
{
   constexpr double monthsPay       = 50.00;
   constexpr double monthsPayDisc   = 0.80;
   constexpr double personalPay     = 30.00;
   constexpr double personalPayDisc = 0.80;
   constexpr double seniorPayDisc   = 0.70;

   double totalMonths = months * monthsPay;
   std::cout << "\tMonthly cost without discount: " << totalMonths << '\n';

   if (months >= 12)
   {
      totalMonths *= monthsPayDisc; // Gets the discount of 20% (80% of cost)
      std::cout << "\tMonthly cost WITH discount: " << totalMonths << '\n';
   }

   double totalPersonal = personal * personalPay;
   std::cout << "\tTraining cost without discount: " << totalPersonal << '\n';

   if (personal > 5)
   {
      totalPersonal *= personalPayDisc; // 80% of cost
      std::cout << "\tTraining cost WITH discount: " << totalPersonal << '\n';
   }

   double totalCost = totalMonths + totalPersonal;
   std::cout << "\tTotal cost without senior discount: " << totalCost << '\n';

   if (senior == true)
   {
      totalCost *= seniorPayDisc; // 70% of cost
      std::cout << "\tTotal cost WITH senior discount: " << totalCost << '\n';
   }

   return totalCost;
}

Welcome to our Fitness Center!
One month membership is $50.00 and personal training is $30.00 per session.
If you are a senior citizen, the discount is 30%
If you membership you paid is 12 or more months, the discount is 20%
If more than five personal training sessions are bought, the  discount is 20%

Please enter how many months: 18
Are you an senior? (Type 1 for yes or 0 for no): 1
How many personal training sessions?: 5

        Monthly cost without discount: 900
        Monthly cost WITH discount: 720
        Training cost without discount: 150
        Total cost without senior discount: 870
        Total cost WITH senior discount: 609

The total cost is $609

If you don't understand the changes I made, ask. :)
Last edited on
Pages: 12