Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}

Mortgage TablePg Results Notepad mV File Edit Format View Help rincipal $22,500 Annual Interest 5.37% Payment Amount $288.83 $288.83 $288.83 $288.83 $288.83 $288.83 $288.83 $288.83 $288.83 $288.83 10 11 $288.83 $288.83 12 $288.83 $288.83 14 $288.83 $288.83 16 $288.83 $288.83 18 $288.83 $288.83 20 21 $288.83 $288.83 22 23 $288.83 $288.83 24 25 $288.83 $288.83 26 27 $288.83 $288.83 28 29 $288.83 $288.83 30 $288.83 Payment $288.83 Term 96 Months Principal Interest $188.14 $100.69 $187.42 $101.41 $186.69 $102.14 $185.96 $102.87 $185.23 $103.60 $184.49 $104.34 $183.75 $105.08 $183.00 $105.83 $182.26 $106.57 $181.51 $107.32 $180.75 $108.08 $179.99 $108.84 $179.23 $109.60 $178.47 $110.36 $177.70 $111.1 $176.93 $111.90 $176.16 $112.67 $175.38 $113.45 $174.60 $114.23 $173.81 $115.02 $173.02 $115.8 $172.23 $116.60 $171.44 $117.39 $170.64 $118.19 $169.83 $119.00 $169.03 $119.80 $168.22 $120.6 $167.40 $121.43 $166.59 $122.24 $165.77 $123.06 $164.94 $123.89 Tax $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 $350.00 Principal Balance $22,661.86 $22,824.44 $22,987.75 $23,151.79 $23,316.56 $23,482.07 $23,648.33 $23,815.32 $23,983.07 $24,151.56 524,320.81 $24,490.81 $24,661.58 $24,833.11 $25,005.41 $25,178.48 $25,352.32 $25,526.94 $25,702.35 $25,878.54 $26,055.51 $26,233.28 $26,4 $26,591.21 $26,771.37 $26,952.35 $27,134.13 $27,316.72 $27,500.13 $27,684.37 $27,869.42 Col

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:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote