### Programming help with monthly compound interest

Hello. I have to write a program for compound monthly interest with a starting monthly deposit, an annual interest rate, and a new balance. My problem is that I cant seem to get the right output. I tried some recommendations but nothings working. I am a complete noob at this. Any and all help would be appreciated.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{

double result = 0.0, rate = 0, dollars;
int months = 12, result;
result = dollars * pow( ( 1 + rate / months ) , ( months * dollars ) );

cout << "Enter monthly deposit:" <<endl;
cin >> dollars;

cout << "Enter the interest rate (in percent):" <<endl;
cin >> rate;

cout << "Enter the time (months):" <<endl;
cin >> months;

while (dollars < lastNumber)
{

}

cout << "The new balance is: " << sum + lastNumber<< endl;

return 0;
}

For this I had to use a template (sum.cpp) which can be found here for the reference (under labs and assigns #1)
http://people.stfx.ca/mvanbomm/cseng/
 My problem is that I cant seem to get the right output.

Wow! With helpful details like that, I'm sure people will be able to fix your problem in no time...
`result = dollars * pow( ( 1 + rate / months ) , ( months * dollars ) );`
You're trying assign a value to result but haven't assigned yet any value to it's operands in the right side. You should put this statements somewhere after you already stored the value on it's operands, the ( rate, months, dollars ... )

 ``12345678910`` ``````int main () //declare the variables // prompt for dollars, rate etc... //assign the value to result ( result = dollars * pow( ( 1 + rate / months ) , ( months * dollars ) ); ) //print result return 0;``````

And one thing: why is there a comma in that assignment statement
Last edited on
You don't have to rude about it... Seriously. I'm frustrated as it is.

If more details is what you need then fine.

result = dollars * pow( ( 1 + rate / months ) , ( months * dollars ) );
this line right here will not go through for it says it has a previous declaration.

When I type in values in the output the result is always 5 or 16. It will only calculate (I think) the first month.

"nervermore28 There is a comma because that is what someone recommended me to do.
Thank you for all your help. It was much appreciated. The program still isn't quite there yet, but its close. Thank you for all your help. I do appreciate it.

especially @ nevermore28 :)

Cheers
lol, i've just realize that pow is a function that takes arguments... and so the COMMA in there is just RIGHT, sorry for wrong info :////
If you are supposed to use sum.cpp and the instructions on the course page, I would use (principle * rate / 1200) in the while loop. It is not correct to use the pow function. The term (principle * rate / 1200) assumes you use 10 instead of 0.1 as a 10% rate. Furthermore it converts the annual interest rate to the monthly rate.

#include <iostream>
using namespace std;

int main()
{
double deposit;
double rate;
unsigned short years;
double sum;

// description and input
cout << "This program determines how much money\n"
<< "a person would have saved if they deposit\n"
<< "a given amount of money each month for a\n"
<< "given number of years into a savings account\n"
<< "which earns interest at a given annual rate.\n\n";
cout << "Please start by entering the monthly deposit: ";
cin >> deposit;
cout << "\nEnter the annual interest rate: ";
cin >> rate;
cout << "\nEnter the number of years: ";
cin >> years;

// calculate future value
unsigned short counter = 1;
sum = 0;
while (counter <= years * 12)
{
sum = sum + sum * (rate / 1200) + deposit;
counter = counter + 1;
}

cout << "\nMoney saved is " << sum << endl;
return 0;
}
Topic archived. No new replies allowed.