Logic Error

I have created a program that creates a loan amortization table. It works but there is some problem in the logic that is causing it to miscalculate the interest.
The output of the file should be this
Starting Balance: $1000.00
Annual Interest Rate: 0.035
Monthly Payment: $120.00
Payment Amount Interest Principal Balance
1 120.00 2.91 117.09 882.91
2 120.00 2.57 117.43 765.48
3 120.00 2.23 117.77 647.71
4 120.00 1.88 118.12 529.59
5 120.00 1.54 118.46 411.13
6 120.00 1.19 118.81 292.32
7 120.00 0.85 119.15 173.17
8 120.00 0.50 119.50 53.67
9 53.82 0.15 53.67 0.00


but when I run it the program gives me this

Starting Balance: 1000
Annual Interest Rate: 0.035
Monthly Payment: 120
Payment Amount Interest Principal Balance
1 120.00 2.92 117.08 877.08
2 120.00 2.56 117.44 754.53
3 120.00 2.20 117.80 632.32
4 120.00 1.84 118.16 510.48
5 120.00 1.49 118.51 388.99
6 120.00 1.13 118.87 267.86
7 120.00 0.78 119.22 147.08
8 120.00 0.43 119.57 26.65
10 26.65 0.08 26.65 0


I have looked over it and can't find it so I am hoping that new eyes might be able to find what I did wrong.
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
 #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

void perform_calculations(ofstream& outfile, int balance, float interest, float payment)
{
float monthlyInterest,totalInterest,newBalance,newPayment,interestPaid ;
int paymentsMade;
outfile<<"Starting Balance: " << balance<<endl<<"Annual Interest Rate: "<<interest<<endl<<"Monthly Payment: "<<payment<<endl;
outfile<<"Payment"<<setw(10)<<"Amount"<<setw(10)<<"Interest"<<setw(10)<<"Principal"<<setw(10)<<"Balance"<<endl;
monthlyInterest=interest/12;
paymentsMade=1;
newBalance=balance;
totalInterest=0;

while (newBalance>=payment){

interestPaid=newBalance*monthlyInterest;
newBalance=newBalance-(payment+interestPaid);
totalInterest=+interestPaid;
newPayment= payment-interestPaid;


outfile<<fixed<<setprecision(2)<<paymentsMade<<setw(10)<<payment<<setw(10)<<interestPaid<<setw(10)<<newPayment<<setw(10)<<newBalance<<endl;
paymentsMade+=1;

}
if(newBalance<payment)
{
interestPaid=newBalance*monthlyInterest;
totalInterest=+interestPaid;
paymentsMade++;

outfile<<fixed<<setprecision(2)<<paymentsMade<<setw(10)<<newBalance<<setw(10)<<interestPaid<<setw(10)<<newBalance<<setw(10)<<"0"<<endl;

}


return;

}
int main() {
string infile_name, outfile_name;
int count_of_items_in_file;
ifstream infile;
ofstream outfile;

cout << "Please enter the input file name: ";
cin >> infile_name;

cout << "The user entered file name: " << infile_name << endl;

cout << "Please enter the output file name: ";
cin >> outfile_name;

cout << "User entered output file name as: " << outfile_name << endl;

infile.open(infile_name.c_str());

if (!infile)
{
cout << "Error: Unable to open file " << infile_name << " for reading." << endl;
cout << "Terminating Program.";
return 1;
}
else


outfile.open(outfile_name.c_str());


if (!outfile)
{
cout << "Error: Unable to open file " << outfile_name << " for writing." << endl;
cout << "Terminating Program.";
infile.close();
return 1;
}
else



count_of_items_in_file = -7654;
infile >> count_of_items_in_file;


for (int i=0; i<count_of_items_in_file; i++)
{

int input1;
float input2, input3;
infile >> input1 >> input2 >> input3;
perform_calculations(outfile, input1, input2, input3);
}

infile.close(); 
outfile.close();
}
((12.035 / 12 * 880) + (0.035 / 12 * 120))
This formula gave me the correct output of 882.91
Topic archived. No new replies allowed.