Formatting Output (setw or right justify)

All I want to do is format the output to where everything is lined up.

I have a program that calculates monthly payments for a loan in one function, and displays an amortization table in another function. Specifically, I need the decimals to line up exactly, and it would be nice if the numbers lined up under their headings as well. Can someone please help me? I obviously don't understand setw.

Here is what my output currently looks like...

Payment Num.   Beg. Balance   Payment   Interest   Paid Principal  New Balance

  1            $120000.00     $922.70   $850.00    $72.70          $119927.30
  2            $119927.30     $922.70   $849.49    $73.21          $119854.09
  3            $119854.09     $922.70   $848.97    $73.73          $119780.36
  4            $119780.36     $922.70   $848.44    $74.25          $119706.11
  5            $119706.11     $922.70   $847.92    $74.78          $119631.33
  6            $119631.33     $922.70   $847.39    $75.31          $119556.03
355            $5401.48     $922.70   $38.26    $884.44          $4517.04
356            $4517.04     $922.70   $32.00    $890.70          $3626.34
357            $3626.34     $922.70   $25.69    $897.01          $2729.33
358            $2729.33     $922.70   $19.33    $903.36          $1825.97
359            $1825.97     $922.70   $12.93    $909.76          $916.21
360            $916.21     $916.21   $6.49    $916.21          $0.00


And here is my code... This is just the function in which I am having trouble formatting the output (where all the setw's are at the very bottom).

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
 void amortizationTable(double &principal, double &interest, double &years, double payment)
{
	int count;		        //Keeps track of number of payments
	double newBalance,		//Beginning balance
	       endBalance,		//Ending balance
	       interestPaid,	        //Amount of interest paid
	       principalPaid;	        //Amount of principal paid
      
        //formatting for decimals
	cout << fixed << showpoint << setprecision(2);

        //setting up table headers
	cout << "Payment Num." << setw(15) << "Beg. Balance" << setw(10) << "Payment";
	cout << setw(11) << "Interest" << setw(17) << "Paid Principal" << setw(15) << "New Balance\n\n";

	endBalance = principal;

	for (count = 1; count < (years * 12 + 1); count++)
	{
		newBalance = endBalance;
		interestPaid = (newBalance * interest) / 12;
		principalPaid = payment - interestPaid;
		endBalance = newBalance - principalPaid;

		if (newBalance < payment)
		{
			payment = newBalance;
			endBalance = 0.00;
		}
		if (count < 7 || count >((years * 12 + 1) - 7))
		{
			cout << fixed << setprecision(2);
			cout << setw(3) << count;
			cout << setw(13) << "$" << newBalance;
			cout << setw(6) << "$" << payment;
			cout << setw(4) << "$" << interestPaid;
			cout << setw(5) << "$" << principalPaid;
			cout << setw(11) << "$" << endBalance << endl;
		}
	}

}
This:
cout << setw(13) << "$" << newBalance;
Is same as this:
1
2
cout << setw(13) << "$";
cout << newBalance;

The setw affects only the $.

The http://www.cplusplus.com/reference/locale/ might be able to format dollars.
Topic archived. No new replies allowed.