Trouble with simple Loop Program

Question: A country club, which currently charges $2,500 per year for membership, has
announced it will increase its membership fee by 4% each year for the next six years.
Write a program that uses a loop to display the projected rates for the next six years.

My main problem is that it says it increased 4% each year, my thought was that I could just do 2500 + (2500 * .04) and it would increment that easily but it couldn't because it just displays the same answer! I am having the trouble incrementing the .04 each time. Thanks! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
	const double years_Pro = 6;
	double membership_Fee = 2500;
	double total_Fees;

	for (double i = 1; i <= 6; i++)
	{
		total_Fees = membership_Fee + (membership_Fee * .04);
		cout << "Year: " << i << " which costs: " << total_Fees << endl;
	}


	return 0;
}
I'm not sure the question is asking for the total fees over the entire period, is it? (Or maybe it is?). But I'd start by just calculating the rate for each year.

Starting fee:
 
    double membership_Fee = 2500;

Next year's fee
 
    membership_Fee = membership_Fee * 1.04;

You'd just need to put that inside the loop, and display the value each time.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
    int years_Pro = 6;
	double membership_Fee = 2500;
	
	for (int i = 0; i < years_Pro + 1; i++)
	{
		cout << "Year: " << i << " which costs: " << membership_Fee << endl;
		membership_Fee *= 1.04;
	}
	return 0;
}
So I am a bit confused on why the "total_Fees" does not increment. I thought if you only use the variable "i" it increments because that's how it works. I changed my code to this, and while it's the same and correct output, I don't know why we are using "membership_Fee" =membership_Fee + (membership_Fee * .04);. How come the total_Fees does not work and the total_Fees does?

Also, another additional question, does the place where the line goes "membership_Fee" matters? I included comments on what I am talking about in the second pieace of code. I would imagine it does, because:

Loop starts, says Year 1: Costs: 2600 (because of the localized variable). Then calculates membership_Fee variable and does the calculation and store it in the membership_Fee variable, and runs the loop again and then the second loop starts saying Year 2: Cost: 2704. This is repeated 6 more times..

Sorry if my logic is wrong but I included comments in the code itself so what I am talking about. Thanks for the tips/help so far! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
	int years_Pro = 6;
	double membership_Fee = 2500;

	for (int i = 1; i < 6; i++)
	{
		cout << "Year: " << i << " which costs: " << membership_Fee << endl;
		membership_Fee = membership_Fee + (membership_Fee * .04);
	}
	return 0;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
	int years_Pro = 6;
	double membership_Fee = 2500;

	for (int i = 1; i < 6; i++)
	{
		membership_Fee = membership_Fee + (membership_Fee * .04); // Can it go here also, or is it practical the other way?
		cout << "Year: " << i << " which costs: " << membership_Fee << endl;

	}
	return 0;
}

Last edited on
Sorry for the additional question and double post, but I was curious that I wanted to find the sum of all the fee, so I decided to use an accumulator. Naturally I thought I could just do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
	int years_Pro = 6;
	double membership_Fee = 2500;
	double accum = 0.0;

	for (int i = 1; i <= 6; i++)
	{
		cout << "Year: " << i << " which costs: " << membership_Fee << endl;
		membership_Fee = membership_Fee + (membership_Fee * .04); 
		accum += membership_Fee; // And not here?
	}

	cout << "Total is: " << accum << endl;
	
	return 0;
}


But gave me the wrong answer in regards to the variable "accum" but as soon as I did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
	int years_Pro = 6;
	double membership_Fee = 2500;
	double accum = 0.0;

	for (int i = 1; i <= 6; i++)
	{
		cout << "Year: " << i << " which costs: " << membership_Fee << endl;
		accum += membership_Fee; // Why does this go here?
		membership_Fee = membership_Fee + (membership_Fee * .04); 
	}

	cout << "Total is: " << accum << endl;
	
	return 0;
}


It works. I don't understand the logic behind this. Is it because it's just getting the total of all the "membership_Fee" from "cout << "Year: " << i << " which costs: " << membership_Fee << endl;"?



Last edited on
closed account (48T7M4Gy)
@OP
1. Going back to your first post membership_Fee remains the same value however many iterations there are. So total_Fees therefore is always the same value.

2. Simple math gives x + .04x = 1.04x

3. If you want the total paid over 6 years then you need to initialise total_Fees = 0 outside the loop
1
2
membership_Fee = membership _Fee * 1.04;
total_Fees = total_Fees + membership_Fee;



Understand it now, thanks! Will mark as resolved.
Topic archived. No new replies allowed.