Investment program

Pages: 12
C++ is like Voodoo to me right now. I need help displaying an investment account for two separate people at two different rates. It needs to be done in a form of a loop. I picked a for loop because the instructor said that is the kind you use when you already know how many times it needs to loop. I know the program is very bare bones and not very well done, I was looking for some guidance.

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
  #include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	double Lprinciple;
	double Jprinciple;
	double Lintrest= .06;
	double Jinterest;
	int age=20;
	cout << "******************** Investing vs. Saving ********************\n\n";
	cout << "Age		Linda's Account		John's Account\n";
	cout << "-------------------------------------------------------------";
	// Loop to display time from 20 to 60 in increments of 10 years.
	for (age = 20; age <= 60; age + 10)
	{
		// Calculate the amount for Linda
		for (Lprinciple = 1000; age <= 60; Lprinciple++)
		{
			Lprinciple = Lprinciple + (Lprinciple*Lintrest)*10;
		}
		// Calculate the amount for John
		for (Jprinciple = 1000; age <= 60; Jprinciple++)
		{
			Jprinciple = Jprinciple + (Lprinciple*Lintrest) * 10;
		}
		// Display the amounts of each account side by side 10 years at a time.
		cout << age << "\t\t$" << Lprinciple << "\t\t$" << Jprinciple;
		cout << endl;
	}
	cin.get();
	return 0;

}
It seems like you have the general idea of what is going on and what to do; what exactly are you having troubles with/what is not working as expected?
Nothing will display besides the header. I have an updated code but still no results.
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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	double Lprinciple;
	double Jprinciple;
	double Lintrest= .06;
	double Jinterest= .015;
	int age=20;
	cout << "******************** Investing vs. Saving ********************\n\n";
	cout << "Age		Linda's Account		John's Account\n";
	cout << "-------------------------------------------------------------";
	// Loop to display time from 20 to 60 in increments of 10 years.
	for (age = 20; age <= 60; age ++)
	{
		// Calculate the amount for Linda
		for (Lprinciple = 1000; age <= 60; Lprinciple++)
		{
			Lprinciple = (Lprinciple + (Lprinciple*Lintrest))*10;
		}
		// Calculate the amount for John
		for (Jprinciple = 1000; age <= 60; Jprinciple++)
		{
			Jprinciple = (Jprinciple + (Lprinciple*Lintrest)) * 10;
		}
		// Display the amounts of each account side by side 10 years at a time.
		
	}
	cout << age << "\t\t$" << Lprinciple << "\t\t$" << Jprinciple;
	cout << endl;
	cin.get();
	return 0;

}
Try checking the conditions of your for loops on line 19/24. Can you see what the problem is there?
I feel like there is something wrong with the test portion of the for loop but I dont know what to put there.
Explain to me what those for loops are doing in English. If you don't understand, then I suggest you go and read up what a for loop is again to refresh your memory.
As far as I understand it you start at some initial value. In this case it would be age, and the value of the investment. You then make some adjustment to this value, adding ten years to the age, and some value to the investment. This will repeat until a certain condition is met, when the age reaches 60 in this case. The outer loop tells the program how long to loop and the inner for loops tell the program what to do while it is looping.
Last edited on
So between lines 19-23, when do you think age will reach 60?
I thought I had a light bulb moment after reading your comment, but no luck. I added something at line 29.
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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	double Lprinciple;
	double Jprinciple;
	double Lintrest= .06;
	double Jinterest= .015;
	int age=20;
	cout << "******************** Investing vs. Saving ********************\n\n";
	cout << "Age		Linda's Account		John's Account\n";
	cout << "-------------------------------------------------------------";
	// Loop to display time from 20 to 60 in increments of 10 years.
	for (age = 20; age <= 60; age ++)
	{
		// Calculate the amount for Linda
		for (Lprinciple = 1000; age < 60; Lprinciple++)
		{
			Lprinciple = (Lprinciple + (Lprinciple*Lintrest))*10;
		}
		// Calculate the amount for John
		for (Jprinciple = 1000; age < 60; Jprinciple++)
		{
			Jprinciple = (Jprinciple + (Lprinciple*Lintrest)) * 10;
		}
		// Display the amounts of each account side by side 10 years at a time.
		age = age + 10;
		
	}
	cout << age << "\t\t$" << Lprinciple << "\t\t$" << Jprinciple;
	cout << endl;
	cin.get();
	return 0;

}
I am off to bed. Thanks for taking the time to try and lead me to the right answer, sorry for being so slow on picking it up.
Just look at lines 19-22 by themselves. Remember that each loop is completely independent of the others. And as long as you eventually get it, you'll be fine, don't worry.
I feel like the inner loops should not have age as their test condition but I do not know what to replace it with.
I am getting a little more information displayed with this code, but I am still way off.
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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	double Lprinciple=1000;
	double Jprinciple=1000;
	double Lintrest= .06;
	double Jinterest= .015;
	double age;
	double i, j;
	cout << "******************** Investing vs. Saving ********************\n\n";
	cout << "Age		Linda's Account		John's Account\n";
	cout << "-------------------------------------------------------------\n";
	// Loop to display time from 20 to 60 in increments of 10 years.
	for (age = 20; age <= 60; age ++)
	{
		age = age + 10;
		// Calculate the amount for Linda
		for (i=0; i<7; i++)
			{
				i = (Lprinciple + (Lprinciple*Lintrest))*10;
			}
		// Calculate the amount for John
		for (j =0; j < 7; j++)
			{
				i = (Jprinciple + (Lprinciple*Lintrest)) * 10;
			}
		// Display the amounts of each account side by side 10 years at a time.
		
		
	}
	cout << age << "\t\t$" << Lprinciple << "\t\t\t$" << Jprinciple;
	cout << endl;
	system("pause");
	return 0;

}
Could any one give me hint on what to use for my inner for loop condition?
This is the best I have so far.
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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
	double Lprinciple;
	double Jprinciple;
	double Lintrest= .06;
	double Jinterest= .015;
	int age=20;
	cout << "******************** Investing vs. Saving ********************\n\n";
	cout << "Age		Linda's Account		John's Account\n";
	cout << "-------------------------------------------------------------\n";
	// Loop to display time from 20 to 60 in increments of 10 years.
	for (age = 20; age <= 60; age+=10 )
	{
		
		// Calculate the amount for Linda
		for (Lprinciple = 1000; Lprinciple>1000; Lprinciple+=10)
		{
			Lprinciple = (Lprinciple + (Lprinciple*Lintrest));
		}
		// Calculate the amount for John
		for (Jprinciple = 1000; Jprinciple>1000; Jprinciple+=10)
		{
			Jprinciple = (Jprinciple + (Lprinciple*Lintrest));
		}
		// Display the amounts of each account side by side 10 years at a time.
		cout << age << "\t\t$" << Lprinciple << "\t\t\t$" << Jprinciple;
		cout << endl;	
		
	}
	
	cin.get();
	return 0;

}
Let us rewrite your Linda for loop in its while loop equivalent:
1
2
3
4
5
6
    Lprinciple = 1000; 
    while (Lprinciple>1000)
    {
	Lprinciple = (Lprinciple + (Lprinciple*Lintrest));
        Lprinciple+=10;
    }
Now the first time into the loop, what is the value of Lprinciple?
At that same time, what is the value of the expression Lprinciple>1000?

The first time into the loop the value of Lprinciple would be 1000. I don't quite know what the value of the expression of the Lprinciple. I just cant figure out what sort of test to replace it with.
The first time into the loop the value of Lprinciple would be 1000. I don't quite know what the value of the expression of the Lprinciple. I just cant figure out what sort of test to replace it with.


Well, first you should write down what you want the code to do in English, and then slowly try to translate into code. Test each part as you are writing it so you can be sure it does what you expect.
firedraco said:
first you should write down what you want the code to do in English, and then slowly try to translate into code.
I strongly agree. (except English or your native language) I suspect that you may only need one for loop. But we will not know until you explain to yourself what this code wants to do.
I actually need this program for my kids homework , ,it saved my time ha ha..intially i searched for program at https://www.youwintube.com/ , http://torrentpanda.com/ , http://www.cplusplus.com/forum/ , http://www.cplus.com
Last edited on
Pages: 12