Help with programming compound interest program

Hi. I need some help with my coding, for I am a beginner and am ready to rip my hair out. I need help getting the output to actually work. The values I've been inputing are 1000 for initial value, 6% in interest, and 1 year. I get 16 which I know is not correct. I would really appreciate it if someone could help me out.

#include <iostream>

using namespace std;

int main()
{
int lastNumber, sum=0, year=0;
double rate=0, dollars;

cout << "Enter monthly deposit:" <<endl;
cin >> dollars;
double (dollars <=1 || dollars >100000);

cout << "Enter the interest rate (in percent):" <<endl;
cin >> rate;
double (rate <.1 || rate > 99) ;

cout << "Enter the time (years):" <<endl;
cin >> year;
if (year<1 || year>50);



sum = dollars * rate/1200;
dollars <1;

while (dollars < lastNumber)
{
sum = (rate/1200 * dollars);
lastNumber = sum + rate;

}

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

return 0;
}


PS. For the assignment I had to do, I had to work off the sum.cpp template which can be found here for reference.
http://people.stfx.ca/mvanbomm/cseng/ under labs and assigns, #1
Can you please explain to me what you are trying to do with these lines
double (dollars <=1 || dollars >100000); double (rate <.1 || rate > 99) ;

Are you trying to input if they are within those ranges?
You're going to need a while loop or do/while.

1
2
3
4
5
6

do
{
     cout << "Enter interest rate(1% to 99% ): ";
     cin >> rate;
} while( rate < 0.1 || rate > 99 );


Same concept for the other one.

Also I do not under stand this if statement.
if (year<1 || year>50);
That says..if true do nothing.

Are you trying to validate like earlier?
If so do as mentioned above.



What are you trying to do with dollar < 1; ?

Also how did you get that formula you are using from the assignment?

Your assignment clearly says

1) Get a monthly deposit
2) Get an [b]annual[/code] interest rate
3) Check the amount after the amount of years

You have the correct inputs except you never said it was annual.

You misunderstood his formula

principle * rate/1200



The formula he got it from is

A = P( 1 + r/n )nt


Basically I have no idea where he came up with the magical 1200 unless he has a fixed time or something.


I would do something like this

To be honest You don't even need a loop really

You could simply do something like this

1
2
3
4

double result = 0.0;
const int MONTHS = 12;
result = dollars * pow( ( 1 + RATE / MONTHS ) , ( MONTHS * TIME ) );


Or modify it to use a loop.

I don't know what I was trying to do. I'm seriously a noobie. I just thought maybe if I put some sort of interval in it would work.

As for his formula I have no idea where he got that either. I know it was a monthly interest rate so isn't the formula somewhat different ?

With dollar < 1 I was trying to get it to stop 0 for the output.

Thank you I appreciate any and all help
Neither of your recommendations worked....

Pow is not recognized under c++ (or maybe it's just my program ?)
#include <cmath>

That should do the trick.
Thank you for our recommendations. However its still not working. The output is still not correct.

I do appreciate all your help though.

Cheers.
What is the output supposed to be and what are you getting?


Maybe try

1
2
3
4
for( int i = 0; i < TIME; ++i )
{
    dollars *= rate / 1200;
}
I'm a noob too, but maybe this will work?

#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.