Alignment issues

I am having problems trying to get the output aligned. I have been working at it all day and have got no where in getting all the columns lined up with all the dollar signs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#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;
	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;
	
	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);
		outFile.precision(2);
		outFile << right << setw(17) << fixed << "$" << emi;
		outFile << right << setw(19) << fixed << "$" << principal;
		outFile << right << setw(16) << fixed << "$" << interest;
		outFile << right << setw(12) << fixed << "$" << tax_pm;
		outFile << right << setw(22) << fixed << "$" << pV << endl;
		totemi += emi;
		totprin += principal;
		totint += interest;
		tottax += tax_pm;
		totpV += pV;
	}

	outFile << right << setw(7) << "Totals";
	outFile.precision(2);
	outFile << right << setw(1) << fixed << "$" << totemi;
	outFile << right << setw(19) << fixed << "$" << totprin;
	outFile << right << setw(16) << fixed << "$" << totint;
	outFile << right << setw(12) << fixed << "$" << tottax;
	outFile << right << setw(22) << fixed << "$" << pV << 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;
}
Topic archived. No new replies allowed.