I cannot get my headings to print out correctly in the file so that they are lin
ID: 3788382 • Letter: I
Question
I cannot get my headings to print out correctly in the file so that they are lined up correctly with the rest of the columns. The input data I am supposed to test is $22500 loan amount, 5.37 interest rate, 96 months, and $350 tax per year. Can someone fix the columns so they line up properly?
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
//Reads the inputs principal amount, rate of interest, and number of payments.
void readInputs(double &presentValue, double &rateOfInterest, int &numOfPayments, double &tax_pm)
{
cout << "Enter the loan amount: ";
cin >> presentValue;
cout << "Enter the yearly rate of interest(as a percentage): ";
cin >> rateOfInterest;
rateOfInterest = (rateOfInterest / 100) / 12;
cout << "Enter the number of payments (in months): ";
cin >> numOfPayments;
cout << "What is the tax amount per year? $";
cin >> tax_pm;
}
//Calculates the EMI per payment, given the inputs.
void EMICalc(double presentValue, double rateOfInterest, int numOfPayments, double &EMI)
{
EMI = (rateOfInterest * presentValue) / (1 - pow((1 + rateOfInterest), (-1 * numOfPayments)));
EMI = floor(EMI * 100 + 0.5) / 100;
}
//Calcualtes the total amount of tax paid each year
void calculateTax(double tax, int numOfPayments)
{
double No_of_Payments_in_years = numOfPayments / 12;
double totalTax = tax * No_of_Payments_in_years;
double tax_pm = totalTax / numOfPayments;
}
void printTable(double pV, double roI, int noP, double emi, double tax_pm)
{
//For including thousand separator
locale system_locale("");
cout.imbue(system_locale);
cin.imbue(system_locale);
ofstream outFile;
//For including thousand separator
outFile.imbue(system_locale);
outFile.open("MortgageTablePgmV2_Results.txt");
outFile << "Principal $" << pV << " Payment $" << emi << endl;
outFile << "Annual Interest " << fixed << setprecision(2) << roI * 12 * 100 << "% Term " << noP << " Months" << endl << endl;
//Printing table header with proper formatting
outFile << left << setw(16) << "Payment" << setw(16) << "Amount" << setw(18) << "Principal" << setw(19) << "Interest" << setw(15) << "Tax" << setw(11) << "Principal Balance" << endl;
double interest, principal;
for (int i = 0; i < noP; i++)
{
interest = pV * roI;
if (interest < 0)
{
interest = 0;
}
principal = emi - interest;
if (principal < 0)
{
principal = 0;
}
pV = pV - principal + tax_pm;
if (pV < 0)
{
pV = 0;
}
//Writing calculated values to file with required format
outFile << right << setw(6) << (i + 1) << setw(11) << fixed << setprecision(2) << "$" << (principal + interest);
outFile << setw(10) << fixed << setprecision(2) << "$" << principal << setw(10) << fixed << setprecision(2) << "$" << interest;
outFile << setw(10) << fixed << setprecision(2) << "$" << tax_pm << setw(10) << fixed << setprecision(2) << "$" << pV << endl;
}
outFile << "Final Payment " << interest + principal << endl;
}
int main()
{
double presentValue, rateOfInterest, EMI, tax, tax_pm;
int numOfPayments;
readInputs(presentValue, rateOfInterest, numOfPayments, tax_pm);
EMICalc(presentValue, rateOfInterest, numOfPayments, EMI);
calculateTax(tax_pm, numOfPayments);
printTable(presentValue, rateOfInterest, numOfPayments, EMI,tax_pm);
double totalInterest = EMI * numOfPayments - presentValue;
cout << "Total interest paid: " << totalInterest << endl;
return 0;
}
Explanation / Answer
//The below c++ file is modified to print the output file in aligned to columns
//The modified code is in bold letters
//amortization.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
//Reads the inputs principal amount, rate of interest, and number of payments.
void readInputs(double &presentValue, double &rateOfInterest, int &numOfPayments, double &tax_pm)
{
cout << "Enter the loan amount: ";
cin >> presentValue;
cout << "Enter the yearly rate of interest(as a percentage): ";
cin >> rateOfInterest;
rateOfInterest = (rateOfInterest / 100) / 12;
cout << "Enter the number of payments (in months): ";
cin >> numOfPayments;
cout << "What is the tax amount per year? $";
cin >> tax_pm;
}
//Calculates the EMI per payment, given the inputs.
void EMICalc(double presentValue, double rateOfInterest, int numOfPayments, double &EMI)
{
EMI = (rateOfInterest * presentValue) / (1 - pow((1 + rateOfInterest), (-1 * numOfPayments)));
EMI = floor(EMI * 100 + 0.5) / 100;
}
//Calcualtes the total amount of tax paid each year
void calculateTax(double tax, int numOfPayments)
{
double No_of_Payments_in_years = numOfPayments / 12;
double totalTax = tax * No_of_Payments_in_years;
double tax_pm = totalTax / numOfPayments;
}
void printTable(double pV, double roI, int noP, double emi, double tax_pm)
{
//For including thousand separator
locale system_locale("");
cout.imbue(system_locale);
cin.imbue(system_locale);
ofstream outFile;
//For including thousand separator
outFile.imbue(system_locale);
outFile.open("MortgageTablePgmV2_Results.txt");
outFile <<left<<setw(20)<<"Principal$" << pV
<<right<<setw(20)<< "Payment$" <<setw(10)<< emi <<endl
<<left<<setw(20)<<"Annual Interest"
<<fixed << setprecision(2) << roI * 12 * 100 <<"%"
<<right<<setw(20)<<
"Months" <<setw(10)<< noP <<endl << endl;
//Printing table header with proper formatting
outFile << left << setw(16) << "Payment"
<< setw(16) << "Amount"
<< setw(16) << "Principal"
<< setw(16) << "Interest"
<< setw(16) << "Tax" << setw(11) << "Principal Balance" << endl;
double interest, principal;
for (int i = 0; i < noP; i++)
{
interest = pV * roI;
if (interest < 0)
{
interest = 0;
}
principal = emi - interest;
if (principal < 0)
{
principal = 0;
}
pV = pV - principal + tax_pm;
if (pV < 0)
{
pV = 0;
}
//Writing calculated values to file with required format
outFile << right << setw(6) << (i + 1) << setw(11) << fixed << setprecision(2) << "$" << (principal + interest);
outFile << setw(10) << fixed << setprecision(2) << "$" << principal << setw(10) << fixed << setprecision(2) << "$" << interest;
outFile << setw(10) << fixed << setprecision(2) << "$" << tax_pm << setw(10) << fixed << setprecision(2) << "$" << pV << endl;
}
outFile << "Final Payment " << interest + principal << endl;
}
int main()
{
double presentValue, rateOfInterest, EMI, tax, tax_pm;
int numOfPayments;
readInputs(presentValue, rateOfInterest, numOfPayments, tax_pm);
EMICalc(presentValue, rateOfInterest, numOfPayments, EMI);
calculateTax(tax_pm, numOfPayments);
printTable(presentValue, rateOfInterest, numOfPayments, EMI,tax_pm);
double totalInterest = EMI * numOfPayments - presentValue;
cout << "Total interest paid: " << totalInterest << endl;
return 0;
}
---------------------------------------------------------------------------------------------------
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.