output formatting error

Hi, I am trying to figure out why my output is coming out non aligned with the header like this:

Month Principal Interest Balance
_____ _________ ________ _______
12 99.59 0.41 99.59
11 99.17 0.83 198.76
10 98.76 1.24 297.52
9 98.35 1.65 395.87
8 97.94 2.06 493.81
7 97.54 2.46 591.35
6 97.13 2.87 688.48
5 96.73 3.27 785.21
4 96.33 3.67 881.53
3 95.93 4.07 977.46
2 95.53 4.47 1072.99
1 95.13 4.87 1168.12

But it is supposed to display it like this:

Month Principal Interest Balance

----- --------- --------- ------

12 99.59 0.41 99.59
11 99.17 0.83 198.76
10 98.76 1.24 297.52
9 98.35 1.65 395.87
8 97.94 2.06 493.81
7 97.54 2.46 591.35
6 97.13 2.87 688.48
5 96.73 3.27 785.21
4 96.33 3.67 881.53
3 95.93 4.07 977.46
2 95.53 4.47 1072.99
1 95.13 4.87 1168.12


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
112
113
114
115
116
117
118
119
120
121
122
  #include <iostream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std; 

double GetParam(string prompt, double min, double max)
{
	double number;
	cout << prompt;

	
	while ((!(cin >> number)) || (number > max || number < min))
	{
		cin.clear();
		cin.ignore();
		cout << prompt;
	}

	return number;
}

void PrintHeader(void)
{
	string month = "Month"; 
	string principal = "Principal"; 
	string interest = "Interest"; 
	string balance = "Balance"; 

	cout << month; 
	cout << setw(15) << principal;
	cout << setw(20) << interest;
	cout << setw(15) << balance;
	cout << endl;

	for (int i = 0; i < month.size(); i++)
	{
		cout << "_";
	}
	
	cout << setw(7);
	for (int i = 0; i < principal.size(); i++)
	{
		cout << "_";
	}
	cout << setw(13); 
	for (int i = 0; i < interest.size(); i++)
	{
		cout << "_"; 
	}
	cout << setw(9); 
	for (int i = 0; i < balance.size(); i++)
	{
		cout << "_"; 
	}
	cout << endl;
}

void PrintMonthlyData(int month, double principal,
	double interest, double balance)
{
	double newbalance, newprincipal, newinterest = 0; 
	vector <double> newbal; 
	vector <double> newprinc; 
	vector <double> newinter; 
	balance = 0; 

	while (newinterest < interest * 100)
	{
		
		newbalance = (balance + principal) / (1 + (interest / 12));
		newbal.push_back(newbalance);

		newprincipal = newbalance - balance; 
		newprinc.push_back(newprincipal);

		balance = newbalance; 
		newinterest = principal - newprincipal;
		if (newinterest > interest * 100)
		{
			break; 
		}

		newinter.push_back(newinterest);
		
		month++;
	}

	int totalmonth = month; 
	
	for (int i = 0; i < totalmonth; i++)
	{
		cout << month; 
		month--; 

		cout << setw(15) << fixed << setprecision(2) << newprinc[i]; 
		cout << setw(20) << setprecision(2) << newinter[i]; 
		cout << setw(15) << setprecision(2) << newbal[i]; 
		cout << endl; 
	}
	return; 
}

int main()
{
	double monthpay, interestrate;
	double year;
	

	monthpay = GetParam("Please enter the monthly payment: ", 1, 100000); //Input monthly payment.
	interestrate = GetParam("Please enter the interest rate: ", 0, 1);	  //Input the Interest Rate.
	year = GetParam("Please enter the duration of the loan, in years: ", 1, 100);	// Input duration of loan (years).
	while (year != (int)year) //Checks to see if years is a integer. 
	{
		year = GetParam("Please enter the duration of the loan, in years: ", 1, 100);
	}
	cout << endl;

	PrintHeader();
	PrintMonthlyData(0, monthpay, interestrate, 0); 
	return 0; 
}
Last edited on
Since you didn't post your examples in code tags to retain the formatting it's hard to say what is wrong. But one thing I see is that you haven't used setw() on the month.

Also you only need to use one call to setprecision() since it will remain in effect until you change it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Please enter the monthly payment: 100
Please enter the interest rate: .3
Please enter the duration of the loan, in years: 12

Month      Principal            Interest        Balance
_____      _________            ________        _______
14          97.56                2.44          97.56
13          95.18                4.82         192.74
12          92.86                7.14         285.60
11          90.60                9.40         376.20
10          88.39               11.61         464.58
9          86.23               13.77         550.81
8          84.13               15.87         634.94
7          82.07               17.93         717.01
6          80.07               19.93         797.09
5          78.12               21.88         875.21
4          76.21               23.79         951.42
3          74.36               25.64        1025.78
2          72.54               27.46        1098.32
1          70.77               29.23        1169.09


With setw(3) used to print the month:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Please enter the monthly payment: 100
Please enter the interest rate: .3
Please enter the duration of the loan, in years: 12

Month      Principal            Interest        Balance
_____      _________            ________        _______
 14          97.56                2.44          97.56
 13          95.18                4.82         192.74
 12          92.86                7.14         285.60
 11          90.60                9.40         376.20
 10          88.39               11.61         464.58
  9          86.23               13.77         550.81
  8          84.13               15.87         634.94
  7          82.07               17.93         717.01
  6          80.07               19.93         797.09
  5          78.12               21.88         875.21
  4          76.21               23.79         951.42
  3          74.36               25.64        1025.78
  2          72.54               27.46        1098.32
  1          70.77               29.23        1169.09


Topic archived. No new replies allowed.