## Help with homework

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

### Help with homework

Hello everyone. Hope everyone is doing well. Im currently taking intro to computer science and I have a simple project that is due tomorrow. I dont know what I am doing wrong. My assignment is:
Write a C++ program to compute a monthly payment for a mortgage loan. Your program will
ask the user to enter annual interest rate, length of the mortgage, and the amount of the principal.
Then compute the monthly payment according to the information that the user entered. Use the
following formula to compute the monthly payment:
M = P(i / (1 – (1 + i)  t))
Where
M is the monthly payment,
P is the principal value,
i is the monthly interest rate,
t is the length of the mortgage in months.
Output the monthly payment including the information the user entered by both displaying on the
screen and to a file. Try to make the input/output from your program correspond as closely as
possible to the sample input/output below.

Here is my code:

Code: Select all
`#include <iostream>#include <fstream>#include <cmath>#include <iomanip>using namespace std;int main ( ){   float i; //interest   int t; //loan years   double p; //principal   float m; //monthly payment      cout<<"***This program calculates a monthly payment for a loan***"<<endl;   cout<<"\nEnter the annual interest -->";   cin>>i;   cout<<"\nEnter the number of years for the loan -->";   cin>>t;   cout<<"\nEnter the principal amount of the loan -->";   cin>>p;   //Below is the formula to compute the monthly payment.   //Formula is M = P(i / (1 – (1 + i) - t))   m = p* i/1*-(pow((1+i),-t));   //The output that will appear on the screen   fstream project6; //fstream to write to file   cout<<"\n****************************************************"<<endl;   cout<<"* A MORTGAGE LOAN CALCULATOR *"<<endl;   cout<<"****************************************************"<<endl;   cout<<"\nThe annual interest rate of your mortgage is "<<i<<"%"<<endl;   cout<<"The length of your mortgage is "<<t<<" years"<<endl;   cout<<"The principal amount of your mortgage is \$"<<p<<endl;   cout<<"Your monthly payment is \$"<<m<<endl;   project6.open("h:\\project6.txt",ios::out);  //written file location flash drive      //change cout to project6 to write file   project6<<"****************************************************"<<endl;   project6<<"* A MORTGAGE LOAN CALCULATOR *"<<endl;   project6<<"****************************************************"<<endl;   project6<<"\nThe annual interest rate of your mortgage is "<<i<<"%"<<endl;   project6<<"The length of your mortgage is "<<t<<" years"<<endl;   project6<<"The principal amount of your mortgage is \$"<<p<<endl;   project6<<"Your monthly payment is \$"<<m<<endl;   project6.close();   cin.get(); cin.get();   return 0;}`

I played with the formula to calculate the monthly payment and it works if I chnage i +1 to just i. But i need to add 1 to the interest. I dont know what I am doing wrong. Is the formula wrong?

Everything else works great the program writes the output to a notepad file in my flash drive. I just cannot get to formula to compute
lp6786
Gerbil

Posts: 64
Joined: Fri Jan 19, 2007 11:24 am
Location: Texas

### Re: Help with homework

//Formula is M = P(i / (1 – (1 + i) - t))

m = p* i/1*-(pow((1+i),-t));

It looks like your formula is wrong. I believe you are missing parentheses after the division (I am also not sure if you need that multiplication after the 1). I think this will fix it but try this:

m = p* i/(1*-(pow((1+i),-t)));

if not that then:

m = p* i/(1-(pow((1+i),-t)));
To Start Press Any Key'. Where's the ANY key?
If something's hard to do, then it's not worth doing
You know, boys, a nuclear reactor is a lot like a woman. You just have to read the manual and press the right buttons.
mmmmmdonuts21
Gerbil Elite

Posts: 593
Joined: Wed Jul 16, 2008 9:09 am

### Re: Help with homework

I have never written any C++, but this page (http://cplus.about.com/od/learning1/ss/cppnumbers.htm) says to initialize variables with values when you declare them.

Also, when I read the problem, I think your code should do the math like this:

m=p* (i/ (1-(pow(1+i),-t)))

also not sure why you have a * after the first 1. Maybe that is some operator I don't know about.
njenabnit
Gerbil Elite

Posts: 644
Joined: Mon Sep 13, 2004 5:33 pm
Location: Oklahoma City, OK

### Re: Help with homework

Ok. T is supposed to equal the loan in months not in years so I fixed that. I had it set to years so i added:

Code: Select all
`t = y * 12`

Right above the formula.

mmmmmdonuts21 I tried the first formula you gave me and it did not work, but the second worked but its not the right answer.
My inputs should be:
Annual interest of 8.5
Years for the loan 30
Principal amount of the loan 85000
My monthly payment should be, according to the assignment, \$653.58.

I am getting 722500.00

njenabnit:

I originally had that formula, but I kept getting an error on visual basic saying:
error C2661: 'pow' : no overloaded function takes 1 arguments

Am I suppose to declare the pow?
lp6786
Gerbil

Posts: 64
Joined: Fri Jan 19, 2007 11:24 am
Location: Texas

### Re: Help with homework

Try adding an extra set of ( ) around the first power value.

m=p* (i/ (1-(pow((1+i),-t))))

Since Pow is supposed to be like pow(x, y) it will need to know that x is a single value and the addition must be performed first inside parenthesis.
njenabnit
Gerbil Elite

Posts: 644
Joined: Mon Sep 13, 2004 5:33 pm
Location: Oklahoma City, OK

### Re: Help with homework

These are things you should really ask the TA and professor.
i7 4790K - Z97 - 16GiB DDR3 - GTX 980 Ti - 256GB SSD - 500GB SSD - 3TB HDD- 24" IPS - Win10 Pro x64
2015 13" rMBP
Sony RX100
DancinJack
Minister of Gerbil Affairs
Gold subscriber

Posts: 2246
Joined: Sat Nov 25, 2006 3:21 pm
Location: Kansas

### Re: Help with homework

Should you be dividing your variable i by 100 because its an interest rate? If you are entering it as a regular number your powers are going to blow up.
To Start Press Any Key'. Where's the ANY key?
If something's hard to do, then it's not worth doing
You know, boys, a nuclear reactor is a lot like a woman. You just have to read the manual and press the right buttons.
mmmmmdonuts21
Gerbil Elite

Posts: 593
Joined: Wed Jul 16, 2008 9:09 am

### Re: Help with homework

m = p* i/(1-(pow((1+i),-t)));

Donuts has it correct. You can use t in years if i is APR (the proper annual rate) and you'll get a yearly payment. Interest has to be fractional, so make sure you divide by 100 at some point. I believe roundoff won't be a problem for realistic interest rates.

Hmm... take the limit as i->0, l'Hopital's gives us p/t.
and as t->inf we get m=p*i

This makes sense. The formula tells us that if you're paying no interest, your monthly payments will pay down the principle, and if your mortgage will last forever you'll be paying the interest every month.
bigfootape
Gerbil

Posts: 12
Joined: Wed Jun 18, 2003 12:43 pm

### Re: Help with homework

In that case then I would also have to divide i by 12 to get the monthly interest rate correct
lp6786
Gerbil

Posts: 64
Joined: Fri Jan 19, 2007 11:24 am
Location: Texas

### Re: Help with homework

YES!!!! I got it damn I am so happy. LOL
So, I guess the problem really was with the Interest Rate.
Thanks guys. I really appreciate the help. I guess I should just post up the finished code, for anyone whoever runs into the same problem.
lp6786
Gerbil

Posts: 64
Joined: Fri Jan 19, 2007 11:24 am
Location: Texas

### Re: Help with homework

Once again thanks everyone for your time. Could not have done it without yall's help.
Here is the finished code:

Code: Select all
`#include <iostream>#include <fstream>#include <cmath>#include <iomanip>using namespace std;int main (void){   double i; //Annual Interest   double p; //Principal of the loan   double m; //Monthly Payment   double im; //Interest per month   double pm; //Principal divided by 12   int y; //Years for the loan   int t; //Months of the Loan      cout<<"***This program calculates a monthly payment for a loan***"<<endl;   cout<<"\nEnter the annual interest -->";   cin>>i;   cout<<"\nEnter the number of years for the loan -->";   cin>>y;   cout<<"\nEnter the principal amount of the loan -->";   cin>>p;   //Below is the formula to compute the monthly payment.   //Formula is M = P(i / (1 – (1 + i)^- t))   t = 12 * y; //Divide the years by the number of months in a year   im = i/12; // Divide i to get the monthly interest rate   im = im / 100; //Divide interest rate by 100      m = p* im/(1-(pow((1+im),-t)));         //The output that will appear on the screen   fstream project6; //fstream to write to file   cout<<fixed<<showpoint<<setprecision(2);   cout<<"\n****************************************************"<<endl;   cout<<"*           A MORTGAGE LOAN CALCULATOR             *"<<endl;   cout<<"****************************************************"<<endl;   cout<<"\nThe annual interest rate of your mortgage is "<<i<<"%"<<endl;   cout<<"The length of your mortgage is "<<y<<" years"<<endl;   cout<<"The principal amount of your mortgage is \$"<<p<<endl;   cout<<"Your monthly payment is \$"<<m<<endl;   project6.open("e:\\project6.txt",ios::out);  //written file location flash drive      //change cout to project6 to write file   project6<<"\n****************************************************"<<endl;   project6<<"*           A MORTGAGE LOAN CALCULATOR             *"<<endl;   project6<<"****************************************************"<<endl;   project6<<"\nThe annual interest rate of your mortgage is "<<i<<"%"<<endl;   project6<<"The length of your mortgage is "<<t<<" years"<<endl;   project6<<"The principal amount of your mortgage is \$"<<p<<endl;   project6<<"Your monthly payment is \$"<<m<<endl;   project6.close();   cin.get(); cin.get();   return 0;}`
lp6786
Gerbil

Posts: 64
Joined: Fri Jan 19, 2007 11:24 am
Location: Texas