Saturday, May 22, 2010

C++ increment question?

This program is supposed to create the complete amortization table for a loan given a payment amount, interest rate and number of payments to make. The formula used to calculate this comes out correct when I do it by hand but when I run the program, it gives me the same balance over and over again until the number of payments runs out. I think the problem lies in the incrementing of "e" to go back into the formula.


#include %26lt;iostream%26gt;


#include %26lt;cmath%26gt;


using namespace std;





int main ()


{


double a, b, c, d, e, bm, y;





cout %26lt;%26lt; "Enter payment amount:" %26lt;%26lt; flush;


cin %26gt;%26gt; a;


cout %26lt;%26lt; "Enter interest rate (9% as 0.09):" %26lt;%26lt; flush;


cin %26gt;%26gt; b;


cout %26lt;%26lt; "Enter number of payments:" %26lt;%26lt; flush;


cin %26gt;%26gt; c;





bm = b/12;


y = pow (( 1 + bm ), (e - c));


d = a * (1 - y) / bm;





for (e = 1; e %26lt;= c; e++)





cout %26lt;%26lt; "After payment #" %26lt;%26lt; e %26lt;%26lt; " the balance on the loan is $" %26lt;%26lt; d %26lt;%26lt; endl;


return 0;


}

C++ increment question?
you need the equations inside the for loop.





try it like this:





for (e = 1; e %26lt;= c; e++)


bm = b/12;


y = pow (( 1 + bm ), (e - c));


d = a * (1 - y) / bm;





cout %26lt;%26lt; "After payment #" %26lt;%26lt; e %26lt;%26lt; " the balance on the loan is $" %26lt;%26lt; d %26lt;%26lt; endl;


}
Reply:you are not changing d in the for loop, so it prints the same amount always


No comments:

Post a Comment