Doubles/floats adding up wrong

This program I'm making is an amortization schedule, and the numbers aren't adding up correctly (in the beginning, it's off by a cent or two, and by the end it's off by a few dollars. I'm sure it has to do with having floats/doubles or the precision. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <math.h>
using namespace std;
using std::setw;

void errorMsg(int errorType);
void displayTable(double &loan, double &APR, int &years, double payment);

void getLoan(double &loan)
{
   cout << "Enter the loan amount: $";
   cin >> loan;
   while (loan <= 0)
   {
      errorMsg(1);
      cout << "Enter the loan amount: $";
      cin >> loan;
   }
      
   return;
}

void getAPR(double &APR)
{
   cout << "Enter the annual percentage interest rate: ";
   cin >> APR;
   while (APR <= 0)
   {
      errorMsg(2);
      cout << "Enter the annual percentage interest rate: ";
      cin >> APR;
   }
   if (APR > 1)
      APR = APR * .01;
   return;
}

void getYears(int &years)
{
   cout << "Enter the loan term (in years): ";
   cin >> years;
   return;
}


void getOption()
{
   cout << "\nOptions:"
        << "\n\t(n) : Enter new values"
        << "\n\t(d) : Display the amortization schedule"
        << "\n\t(q) : Quit";
   return;
}

void calculatePmt(double principal, float APR, int years, double& payment)
{
   payment = 0.0;
   payment = (principal * (APR / 12)) / (1 - pow(1 + (APR / 12.0),(-12.0 * years)));
   cout << "\nThe monthly mortgage payment is $";
   cout << payment << endl;
   return;
}


void interact(double &loan, double &APR, int &years, double& payment)
{
   char option;
   do 
   {
      getOption();

      do
      {
         cout << "\n> ";
         cin >> option;
         switch (option)
         {
            case 'n':
               cout << endl;
               getLoan(loan);
               getAPR(APR);
               getYears(years);
               calculatePmt(loan, APR, years, payment);
               break;
            case 'd':
               cout << endl;
               displayTable(loan, APR, years, payment);
               break;
            case 'q':
               return;
               break;
         }
      }
      while (option != 'n' 
         && option != 'd' 
         && option != 'q');
   }
   while (option != 'q');
   return;      
}

void errorMsg(int errorType)
{
   cout << "\nERROR: ";
   
   switch (errorType)
   {
      case 1:
         cout << "Please enter a number greater than 0\n";
         break;
      case 2:
         cout << "Please enter a valid interest rate\n";
         break;
      case 3:
         break;
      case 4:
         break;
      case 5:
         break; 
   }

   return;
}


void displayTable(double &principal, double &APR, int &years, double payment)
{
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(2);

   double newInterest = principal * (APR / 12);
   double newPrincipal = payment - newInterest;
   double balance = principal - newPrincipal;
   double totalInterest = newInterest;
   double totalPrincipal = newPrincipal;

   int months = years * 12;
   
   cout << "Month   Principal   Interest   Ttl Principal   Ttl Interest    "
        << "Balance\n";
   cout << "-----   ---------   --------   -------------   ------------   "
        << "----------\n";
   for (int i = 1; i <= months; i++)
   {
      cout << setw(5) << i << setw(12) << newPrincipal << setw(11) << newInterest
           << setw(16) << totalPrincipal << setw(15) << totalInterest 
           << setw(13) << balance << endl;
      newInterest = balance * (APR / 12);
      balance = balance - newPrincipal;
      newPrincipal = payment - newInterest;
      totalPrincipal = totalPrincipal + newPrincipal;
      totalInterest = totalInterest + newInterest;
   }
}

/**********************************************************************
* Add text here to describe what the function "main" does. Also don't forget
* to fill this out with meaningful text or YOU WILL LOOSE POINTS.
***********************************************************************/
int main()
{
   cout << "\nATH Mortgage Calculator 2011\n";
   
   double loan = 0.0;
   double APR = 0.0;
   int years = 0;
   int errorType = 0;
   double payment = 0.0;

   getLoan(loan);
   getAPR(APR);
   getYears(years);
   calculatePmt(loan, APR, years, payment);
   interact(loan, APR, years, payment);

   return 0;
}
many times if I had a math problem I start making sure my equations are right. Maybe even making sure the order of operation isn't screwing me up.

I have been known to break up equations in individual statements to see if I had what I expect in each piece.

It could also be as simple as this change:
 
payment = (principal * (APR / 12)) / (1 - pow(1 + (APR / 12.0),(-12.0 * (double)years)));
but I dont think it is in that equation or is it? Because the monthly payment comes out right (If I input loan as 100000, and apr as .04, and years as 20, the payment should be $605.98, which it is.
puzzling through your code....
1
2
   double totalInterest = newInterest;
   double totalPrincipal = newPrincipal;


Wouldn't these start at zero and let the loop handle it?

You never mentioned how far your math was off. From this it would be an entire payment off, if I assume correctly.

I keep looking at this and it seems to be missing a truth of an amortization table. Where at the beginning of the Table the payment would consist of larger interest portion than the principle paid, and at the end of the table it would be reversed. The Principle paid would be greater than the interest portion. I can see the math you do but I don't see this handled anywhere. These payments in an amortization table may be the same for the life of the loan but how the interest and balance paid changes over the life of the loan.

I hope I explained that clearly.
Topic archived. No new replies allowed.