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.
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace 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;
}
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace 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;
}
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.
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace 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;
}
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.
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace 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;
}
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace 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;
}
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.
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.