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





