formula problem

Hey guys,so my program is supposed to calculate a mortgage monthly payment, also figure out the total amount paid. My problem is in the total payment is cutting off then cents, is there something wrong with the formulas? Ive hit a wall and any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
double calculate_mortgage(double loan_amount, double yearly_rate, int num_years) {
	double monthly_rate = (yearly_rate / 100.0) / 12;
	double num_payments = num_years * 12;
	double monthly_payment = monthly_rate * pow((1 + monthly_rate), num_payments)/ (pow((1+ monthly_rate), num_payments) - 1) * loan_amount;

	return monthly_payment;

}

Last edited on
> My problem is in the total payment is cutting off then cents
sorry, my english is no good, ¿could you please reformulate?

If your problem is in the output, then provide the client code and an example input and output.


By the way, line 8 would never execute
My first post is of the function with most of the formulas, this is where it is being called.

1
2
3
4
5
6
		loan_amount = valid_int("Please enter the loan anount: ");
		yearly_rate = valid_int("Please enter the intrest rate: ");
		num_years = valid_int("Please enter the number of years of the loan: ");
		monthly_payment = calculate_mortgage(loan_amount, yearly_rate, num_years);
		double total_payment = monthly_payment * num_years * 12;
		cout << monthly_payment << " " << total_payment << endl;


the out put is:

Please enter the loan anount: 250000
Please enter the intrest rate: 5
Please enter the number of years of the loan: 30
The monthly payment is: 1342.05
The total amount paid is: 483139 (this should be 483139.46)
I think the issue is that num_years is an integer.

According to the following link it makes me thing that that is right:
http://stackoverflow.com/questions/6041295/multiplication-of-double-with-integer-precision

-Hirokachi

nope, use a debugger to inspect variables.
It holds the right amount, the problem is with the display
http://www.cplusplus.com/reference/iomanip/setprecision/
You're right sorry about that.
Thank you you were right, using;

 
cout.precision(2); cout << fixed << monthly_payment << " " << total_payment << endl;


took care of it. Thank you for the help!
Topic archived. No new replies allowed.