Here's the assignment requirement:
Write a program that creates a series of one or more loan amortization schedules. The program should query the user for an input file name and an output file name. The input file will contain the number of schedules that need to be generated as well as the information needed to generate each schedule (initial loan principal, annual percentage rate, and monthly payment). Each generated amortization schedule should be well formatted and should include a breakdown of the principal and interest for each payment, the total number of monthly payments, and the total interest paid for the life of each loan. This information should be printed to the screen as well as sent to the output file.
I was pretty much procrastinating and waited until the last minutes to actually download and run the compiler and start writing the program (still in break mood :( ), so I missed one detail in the assignment asking me to print the information to the screen in addition to sending them to the output file. Though after submitting the homework, I'm still not sure how to do that. Given the codes below, do I really have to type everything within the second function again to print out the information? Or is there any smarter way to deal with this kinda problem? I know that the codes are unnecessarily long and ugly, but I didnt have much time to think of another way to do it...
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
bool balancecheck(double, double); // check if balance < monthly payment
void getdata(ifstream&, double [][3], int); // Input data from file to 2D array
void printdata(ofstream&, double [][3], int);
int main()
{
ifstream inputfile;
ofstream outputfile;
string filename;
int n; // Number of entries
double table[20][3]; // Create an array. Assume the number of entries wont
// exceed 20. row = number of entries.
//column = in order: principal, annual interest, monthly payment
cout << "Please enter the file name: ";
cin >> filename;
// Open input file
inputfile.open(filename.c_str());
inputfile >> n; // Input number of entries to n
getdata(inputfile, table, n); // Input data from input file to array, using n
inputfile.close();
// Close input file
// Open output file
outputfile.open("output.txt");
printdata(outputfile, table, n); // Calculate given data and print them to output file
outputfile.close();
// Close output file
cout << "Done!";
return 0;
}
//I. Function to get input from input file and put them to array
void getdata(ifstream& inputfile, double table[][3], int n)
{
for (int i = 0; i < n; i++)
{
inputfile >> table[i][0];
inputfile >> table[i][1];
inputfile >> table[i][2];
}
}
//II. Function to calculate given data and print results to output file
void printdata(ofstream& outputfile, double table[][3], int n)
// [][0] = principal
// [][1] = annual interest rate
// [][2] = monthly payment
{
for (int i = 0; i < n; i++) // i = row, n = number of entries
{
// Variables to be used
double monthly_rate, monthly_payment, monthly_interest,
balance, principal, total_interest = 0;
int flag = 0;
// I. Print out the given data
outputfile << fixed << showpoint << setprecision(3);
outputfile << endl << "Starting Balance: $" << table[i][0];
outputfile << endl << "Annual Interest Rate: " << table[i][1];
outputfile << endl << "Monthly Payment: $" << table[i][2] << endl << endl;
// II. Calculate given data and print results
monthly_rate = table[i][1] / 12; // Annual rate divided by 12
monthly_payment = table[i][2];
balance = table[i][0];
outputfile << left << setw(10) << "Payment" << setw(20) << "Amount"
<< setw(15) << "Interest" << setw(20) << "Principal"
<< setw(20) << "Balance" << endl; // Print the headline
outputfile << fixed << showpoint << setprecision(2);
// print the results of calculation
for (int line = 1; flag != -1; line++)
{
if ( balancecheck(monthly_payment, balance) )
{
monthly_interest = balance * monthly_rate;
principal = monthly_payment - monthly_interest;
balance = balance - principal;
total_interest += monthly_interest;
outputfile << left << setw(10) << line << setw(20) << monthly_payment
<< setw(15) << monthly_interest << setw(20) << principal
<< setw(20) << balance << endl;
}
else
{
monthly_interest = balance * monthly_rate;
principal = balance;
monthly_payment = principal + monthly_interest;
balance = principal - balance;
total_interest += monthly_interest;
outputfile << left << setw(10) << line << setw(20) << monthly_payment
<< setw(15) << monthly_interest << setw(20) << principal
<< setw(20) << balance << endl << endl;
outputfile << "Total Payments Made: " << line << endl;
outputfile << "Total Interest Paid: $" << total_interest << endl;
flag = -1;
}
}
}
}
//III. Function to check if balance < monnthly payment
bool balancecheck(double monthly_payment, double balance)
{
int x, y; // x = monthly payment, y = balance
bool cont = true; // cont = continue
x = static_cast <int> (monthly_payment);
y = static_cast <int> (balance);
if (x > y)
{
cont = false;
}
return cont;
}
For the input file:
The input file is a text file that contains integers and doubles. Each number is on its own line. You can create this file using notepad or your favorite text editor. The first entry is an integer that defines the number of schedules that need to begenerated using the data in the file. Each set of three lines after the first line will contain the data for a single amortization schedule. In order, they will be the principal, annual interest rate, and the monthly payment.
And heres my input file
4
1000.00
0.035
120.00
2300.00
0.015
350.00
3200.00
0.027
300.00
1750.00
0.042
385.00