I need help aligning the format for the output Some columns

I need help aligning the format for the output. Some columns appear to line up perfectly and some are not aligning properly. Teacher also wants us to also total up each of the columns at the end of the report:

#include
#include
#include
#include

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;
tax_pm = tax_pm / 12;
}

//Calculates the EMI per payment, given the inputs.
void EMICalc(double presentValue, double rateOfInterest, int numOfPayments, double tax_pm, double &EMI)
{
EMI = (rateOfInterest * presentValue) / (1 - pow((1 + rateOfInterest), (-1 * numOfPayments)));
EMI = floor(EMI * 100 + 0.5) / 100;
EMI = EMI + tax_pm;
}

//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\\t$\" << pV << \"\\t\\tPayment\\t$\" << fixed << setprecision(2) << emi << endl;
outFile << \"Annual Interest\\t\" << fixed << setprecision(2) << roI * 12 * 100 << \"%\\t\\tTerm\\t\" << noP << \" Months\" << 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(10) << \"Principal Balance\" << endl;

double interest, principal;

for (int i = 0; i < noP; i++)
{
  interest = pV * roI;
  if (interest < 0)
  {
   interest = 0;
  }
  principal = (emi - interest) - tax_pm;
  if (principal < 0)
  {
   principal = 0;
  }
  pV = pV - principal;
  if (pV < 0)
  {
   pV = 0;
  }
  //Writing calculated values to file with required format
  outFile << right << setw(6) << (i + 1) << setw(11) << fixed << setprecision(2) << \"$\" << emi;
  outFile << right << setw(10) << fixed << setprecision(2) << \"$\" << principal << setw(10) << fixed << setprecision(2) << \"$\" << interest;
  outFile << right << setw(10) << fixed << setprecision(2) << \"$\" << tax_pm << setw(10) << fixed << setprecision(2) << \"$\" << pV << endl;
}
outFile << \"Final Payment\\t\" << emi << endl;
}
int main()
{
double presentValue, rateOfInterest, EMI, tax, tax_pm;
int numOfPayments;
readInputs(presentValue, rateOfInterest, numOfPayments, tax_pm);
EMICalc(presentValue, rateOfInterest, numOfPayments, tax_pm, 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 $318.00 $318.00 65 66 $318.00 $318.00 67 68 $318.00 $318.00 69 70 $318.00 $318.00 71 72 $318.00 $318.00 73 74 $318.00 $318.00 75 76 $318.00 $318.00 78 $318.00 $318.00 79 80 $318.00 $318.00 81 82 $318.00 $318.00 83 84 $318.00 $318.00 85 86 $318.00 $318.00 87 88 $318.00 $318.00 89 90 $318.00 $318.00 91 92 $318.00 $318.00 93 94 $318.00 $318.00 95 96 $318.00 Final Payment 318.00 $278.43 $279.54 $280.66 $281.79 $282.92 $284.05 $285.19 $286.34 5287.49 $288.65 $289.8 $290.97 $292.15 $293.32 $294.51 $295.69 5296.89 $298.08 $299.29 3300. 5e $301.71 $302.93 $304.15 $305.38 $306.62 $307.86 $309.11 $310.36 $311 62 $312.88 $314.15 $315.43 5316.7 $39.57 $38.45 $37.33 $36.21 $35.08 $33.94 $32.80 $31.66 $30.5 $29.35 $28.19 $27.02 $25.85 $24.67 $23.49 $22.30 $21.11 $19.91 $18.7 $17.50 $16.29 $15.07 $13.84 $12.61 $11.38 $10.13 58.89 $7.63 $6.38 $5.11 $3.84 $2.57 $1.29 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $29.17 $8,593.24 $8,342.87 $8,091.37 $7,838.75 $7,585.00 $7,330.11 $7,074.08 $6,816.91 $6,558.59 $6,299.11 $6,038.47 $5,776.66 $5,513.68 $5,249.52 $4,984.18 $4,717.66 $4,449.94 $4,181.02 $3,910.90 $3,639.57 $3,367.03 $3,093.27 $2,818.28 $2,542.06 $2,264.6 $1,985.91 $1,705.97 $1,424.77 $1,142.32 $858.60 $573.61 $287.35 50.00 Ln 6, Col 44

Solution

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <sstream>

using namespace std;

string replaceCharFrmLast(string str, char ch1, char ch2) {
for (int i = str.length()-1; i >=0 ; i--) {
if (str[i] == ch1){
str[i] = ch2;
return str;
}
}
}

//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;
tax_pm = tax_pm / 12;
}
//Calculates the EMI per payment, given the inputs.
void EMICalc(double presentValue, double rateOfInterest, int numOfPayments, double tax_pm, double &EMI)
{
EMI = (rateOfInterest * presentValue) / (1 - pow((1 + rateOfInterest), (-1 * numOfPayments)));
EMI = floor(EMI * 100 + 0.5) / 100;
EMI = EMI + tax_pm;
}
//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\\t$\" << pV << \"\\t\\tPayment\\t$\" << fixed << setprecision(2) << emi << endl;
outFile << \"Annual Interest\\t\" << fixed << setprecision(2) << roI * 12 * 100 << \"%\\t\\tTerm\\t\" << noP << \" Months\" << 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(10) << \"Principal Balance\" << endl;
double interest, principal;
double totemi=0,totprin=0,totint=0,tottax=0,totpV=0;
std::stringstream buffer;
buffer.precision(2);

for (int i = 0; i < noP; i++)
{
interest = pV * roI;
if (interest < 0)
{
interest = 0;
}
principal = (emi - interest) - tax_pm;
if (principal < 0)
{
principal = 0;
}
pV = pV - principal;
if (pV < 0)
{
pV = 0;
}
//Writing calculated values to file with required format
outFile << right << setw(7) << (i + 1) ;
buffer<< right << setw(16) << fixed << emi;
outFile<<replaceCharFrmLast(buffer.str(),\' \',\'$\');
buffer.str(\"\");
buffer << right << setw(18) << fixed << principal;
outFile<<replaceCharFrmLast(buffer.str(),\' \',\'$\');
buffer.str(\"\");
buffer << right << setw(15) << fixed << interest;
outFile<<replaceCharFrmLast(buffer.str(),\' \',\'$\');
buffer.str(\"\");
buffer << right << setw(11) << fixed << tax_pm;
outFile<<replaceCharFrmLast(buffer.str(),\' \',\'$\');
buffer.str(\"\");
buffer << right << setw(21) << fixed << pV<<endl;
outFile<<replaceCharFrmLast(buffer.str(),\' \',\'$\');
buffer.str(\"\");
totemi+=emi;
totprin+=principal;
totint+=interest;
tottax+=tax_pm;
totpV+=pV;
  
}
outFile << right << setw(7) << \"Total:\" ;
outFile.precision(2);
buffer << right << setw(16) << fixed << totemi;
outFile<<replaceCharFrmLast(buffer.str(),\' \',\'$\');
buffer.str(\"\");
buffer << right << setw(18) << fixed << totprin;
outFile<<replaceCharFrmLast(buffer.str(),\' \',\'$\');
buffer.str(\"\");
buffer << right << setw(15) << fixed << totint;
outFile<<replaceCharFrmLast(buffer.str(),\' \',\'$\');
buffer.str(\"\");
buffer << right << setw(11) << fixed << tottax;
outFile<<replaceCharFrmLast(buffer.str(),\' \',\'$\');
buffer.str(\"\");
buffer << right << setw(21) << fixed << pV<<endl;
outFile<<replaceCharFrmLast(buffer.str(),\' \',\'$\');
buffer.str(\"\");
outFile << \"Final Payment\\t $\" << emi << endl;

}
int main()
{
double presentValue, rateOfInterest, EMI, tax, tax_pm;
int numOfPayments;
readInputs(presentValue, rateOfInterest, numOfPayments, tax_pm);
EMICalc(presentValue, rateOfInterest, numOfPayments, tax_pm, 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;
}

I need help aligning the format for the output. Some columns appear to line up perfectly and some are not aligning properly. Teacher also wants us to also total
I need help aligning the format for the output. Some columns appear to line up perfectly and some are not aligning properly. Teacher also wants us to also total
I need help aligning the format for the output. Some columns appear to line up perfectly and some are not aligning properly. Teacher also wants us to also total
I need help aligning the format for the output. Some columns appear to line up perfectly and some are not aligning properly. Teacher also wants us to also total
I need help aligning the format for the output. Some columns appear to line up perfectly and some are not aligning properly. Teacher also wants us to also total

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site