Trying to do a compounded interest program

D

Drakscon

Im trying to get the compounded interest to go back only being
compounded annually. I want the user of this program to be able to do
semi annually of monthly or some other.

This is my code. Any input is welcome

int main()
{
double a,b,c,d,e;//a is amount to be invested, b is years invested
//c is compound time, d is interest rate, e is the final ammount
char ch;//allows user to enter another investment
do{
system("cls");
cout<<"enter an ammount to invest ";
cin>>a;
cout<<"Enter the years to allow to mature ";
cin>>b;
cout<<"How many times a year will it be compounded? ";
cin>>c;
cout<<"What is the rate ";
cin>>d;

for(int i=0;i<b;i++){
a=a+(a*(d/100));
e=pow(a,c);


}

cout<<"After "<<b<<" years. You have now "<<e<<" dollars in the
account\n";

cout<<"Would you like to run another? ";
cin>>ch;
}while(ch=='y'||ch=='Y');



return 0;
}
 
J

Jim Langston

Drakscon said:
Im trying to get the compounded interest to go back only being
compounded annually. I want the user of this program to be able to do
semi annually of monthly or some other.

Then you need the user to be able to select the formula they want
This is my code. Any input is welcome

int main()
{
double a,b,c,d,e;//a is amount to be invested, b is years invested
//c is compound time, d is interest rate, e is the final ammount
char ch;//allows user to enter another investment
do{
system("cls");
cout<<"enter an ammount to invest ";
cin>>a;
cout<<"Enter the years to allow to mature ";
cin>>b;
cout<<"How many times a year will it be compounded? ";
cin>>c;
cout<<"What is the rate ";
cin>>d;

for(int i=0;i<b;i++){
a=a+(a*(d/100));
e=pow(a,c);

This is your formula. Which will change for differnt types of interest.
What I would do is either make this a function or put it in a class (since
it is c++) then select the appropriate function depending on the users
input. Something like:

double SimpleInterest( double Amount, double Years, double Times, double
Rate )
{
double Balance
for (int i=0; i < b; i++ )
{
Amount = Amount + (Amount * ( Rate / 100.0 ));
Balance = pow(Amount, Times);
}
}

(Note, I don't know if your formula is right)

Then you can:

double Balance = SimpleInterest( a, b, c, d );
You REALLY need to pick better variable names. a, b, c and d are
meaningless. See I used what they actually are.

Anyway, have the user select the type of interest they want then either use
an if else block or switch statement.

double Balance = 0;
if ( Type == "S" ) // Simple Interest
Balance = SimpleInterest( a, b, c, d );
else if ( Type == "C" ) // Compound interest
Balance = CompoundInterest( a, b, c, d );
else if ( ... yada yada )
else
std::cout << "Unknown interest type";

Or whatever.

Of course you'll need to make different functions with the right math for
each type of interest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top