"Zero" output in mortgage calculator

Any explanations for "0" output? (the program asks for a loan increasing by an annual interest rate each month, and how much the man must pay on average monthly in order to pay off the debt)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include <iostream>
 #include <math.h>
 using namespace std;
 int main() {
 //loan size P, interest rate R and length of time to pay out L in months. Want required monthly payment M rounded up to whole dollars.
double p=1;
 int r, l;
 int count=0;
 int m=0;
 cin>>p>>r>>l;
do{
     p=p*(r/12/100);
     m=p/l+m;
     p=p-m;
     l--;
     count++;
 } while (l>0);
     cout<<round(m/count); /*because m=p/l+m calculates the entire monthly value paid, and count is how many months, so m/count must be the average monthly amount) */
 } 
Last edited on
The problem is certainly the integer division (i.e. without decimal point) like this: (r/12/100)
I fixed that but now I got ridiculously large outputs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
 #include <math.h>
 using namespace std;
 int main() {
 //loan size P, interest rate R and length of time to pay out L in months. Want required monthly payment M rounded up to whole dollars.
double p=1;
 double r, l;
 int m=0, sum=0;
 cin>>p>>r>>l;
 int months = l;
do{   p=p*(r/0.12);
     m=p/l;
     sum = sum+m;
     p=p-m;
     l--;
 } while (l!=0);
     cout<<(sum/months); //because sum calculates the entire monthly value paid, and that divided by length is average amount.
 } 


Input: 3100000 2 180
Output: 7853770
Expected: 19949
Last edited on
Are you sure line 11 shouldn't be:
 
do{   p=p*(r/1200);

?

EDIT:

You've made it r/(12/100) when I think it should be (r/12)/100.
Last edited on
Sorry, my interpretation and poor math implementation resulted in a way-off answer. This is closer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
 #include <math.h>
 using namespace std;
 int main() {
 //loan size P, interest rate R and length of time to pay out L in months. Want required monthly payment M rounded up to whole dollars.
double p=1;
 double r, l;
 int m=0, sum=0;
 cin>>p>>r>>l;
 int months = l;
do{   p=p* (1+(r/12/100));
     m=p/l;
     sum = sum+m;
     p=p-m;
     l--;
 } while (l!=0);
     cout<<sum/months;
 } 


intput: 4600000 12 120
output: 74219
expected: 65997
hmmm... getting closer... All my outputs seem a bit too high...
Last edited on
bump. Any thinking on my problem?
Hi,

m is of type int, so on line 12, it gets truncated, not rounded up. This small numerical error can result is larger numerical errors. Be very wary of integer division.

In c+=11 there is the std::round function, which works with doubles.
http://en.cppreference.com/w/cpp/numeric/math/round



Is there any reason all of your variables can't be doubles?

If the number of times to loop is known, then use a for loop.

Good Luck !!

Edit:

You should be able to set a warning in your compiler, about narrowing of your type information. With g++ routinely compile with -Wall -Wextra -pedantic. The cpp shell used on this site has these compiler flags. The shell can be used when a complete program with code tags is posted, via the gear icon on the top right of the code.

Edit2:

-Wconversion is the option I was thinking of, and apparently isn't turned on by the options I mentioned above. Hopefully there is a similar option for your compiler.
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
Last edited on
put them ALL as doubles yet still failed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
 #include <math.h>
 using namespace std;
 int main() {
 //loan size P, interest rate R and length of time to pay out L in months. Want required monthly payment M rounded up to whole dollars.
double p=1;
 double r, l;
 double m=0, sum=0;
 cin>>p>>r>>l;
 double months = l;
do{   p=p* (1+(r/12/100));
     m=p/l;
     sum = sum+m;
     p=p-m;
     l--;
 } while (l!=0);
     cout<<ceil(sum/months);
 } 


intput: 2800000 15 162
output: 56014
expected: 40400

hmmm... maybe I misinterpreted the problem, or have a bad solution?
The problem: http://www.codeabbey.com/index/task_view/mortgage-calculator
Hi,

Is it because you are doing ceil at the end rather on the monthly payment at line 12?
Also, it's #include <cmath> , not #include <math.h>. The latter is the c math library, the former is the C++ math library.
This isn't going to fix your problem, but can I recommend you give your variables more descriptive names, so that it's easy to see at a glance what they all represent? Rather than giving them cryptic, single-letter names, and then having to refer to a comment at the start of the program to see what they mean?

EDIT: Also, if the problem is that you're not getting the values you expect, then I recommend that you learn how to step through the code with a debugger. That way, you can see how the values of your variables change as the program executes each statement in turn, and you can see exactly when and how they start to differ from what you expect.
Last edited on
is this better? (doesn't fix problem)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
 #include <cmath>
 using namespace std;
 int main() 
 {
double loan=1;
 double interest, length;
 double monthlypayment=0, sum=0;
 cin>>loan>>interest>>length;
 double months = length;
do
    {   loan=loan* (1+(interest/12/100));
     monthlypayment=loan/length;
     sum = sum+monthlypayment;
     loan=loan-monthlypayment;
     length--;
     } while (length!=0);
      cout<<ceil(sum/months);
 } 
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
	double loan = 0;
	int length = 0;
	double monthlypayment = 0;
	double rate = 0, factor = 1;

	std::cin >> loan >> rate >> length;
	rate = rate / 100 / 12;

	do
	{
		factor *= (1 + rate);
		length--;
	} while (length != 0);

	monthlypayment = loan * rate * factor / (factor - 1);

	std::cout << monthlypayment << '\n';

}
//https://en.wikipedia.org/wiki/Amortization_calculator 
Last edited on
is this better? (doesn't fix problem)

Yes, that's much more easy to understand.

Straight away, I can see that you're calculating a new monthly payment amount every month. Is that really what you want to do?

Finding out anything useful from the debugger?
how intriguing.. the other guy's problem solved it... I guess I must have misinterpreted the problem and/or used the wrong equation.
Topic archived. No new replies allowed.