Formatted output problem

Hi,

I'm having trouble using 'setw', 'fixed' and 'setprecision()' to get the output to format as I want. The code below is my payslip class. I have an 'employee' class which this class caters to. It outputs a formatted payslip for each employee (of which there are four). However, once I run the program, the vertical bars placed at the end of each line are planted different for each employee. Is there anyway to ensure the vertical bars sit in place, regardless of the number displaying?

Forgive me if I am explaining this badly.
Any help would be greatly appreciated.

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
  #include <iostream>
#include <iomanip>
#include "Netpay_ac.h"
using namespace std ;


float net_payslip_class::net_payslip(int emp_no_p, float gross_p, float prsi_p, float net_p, float vhi_p,
                                     float usc_p, float union_p, float paye_p )
{

        dm_employee_no = emp_no_p;
        dm_gross = gross_p;
        dm_prsi = prsi_p;
        dm_net = net_p;
        dm_paye = paye_p;
        dm_vhi = vhi_p;
        dm_usc = usc_p;
        dm_union = union_p;


        cout << setw(10) << "------------------------------------------------"  << endl;
        cout << setw(10) << "|                  PAYSLIP                     |"  << endl;
        cout << setw(10) << "| Employee number: " << dm_employee_no << "                         |" << endl;
        cout << setw(10) << "|                                              |"  << endl;
        cout << setw(10) << "|                                              |"  << endl;
        cout << setw(10) << "|Gross pay : "    << fixed << setprecision(2) << dm_gross << setw(28) << "|" << endl;
        cout << setw(10) << "|                                              |"  << endl;
        cout << setw(10) << "| PAYE  :           " << fixed << setprecision(2)  << dm_paye << setw(22) << "|" <<  endl;
        cout << setw(10) << "| PRSI  :           " << fixed << setprecision(2)  << dm_prsi << setw(23) << "|" <<  endl;
        cout << setw(10) << "| USC   :           " << fixed << setprecision(2)  << dm_usc << setw(23)   << "|" <<  endl;
        cout << setw(10) << "| Union :           "  << fixed << dm_union << setw(23) <<  "|" <<  endl;
        cout << setw(10) << "| VHI   :           "  << fixed << dm_vhi   << setw(23) << "|" <<  endl;
        cout << setw(10) << "| Netpay:           "  << fixed << dm_net   << setw(22)  << "|" <<  endl;
        cout << setw(10) << "|                                              |"  << endl;
        cout << setw(10) << "------------------------------------------------"  << endl;


}
Last edited on
First if the output you're trying to print is larger than the value you supply to setw() you will get the larger output. The setw() function only adds the fill character when the output is smaller than the value. So in your snippet the first setw() probably doesn't appear to do anything.

Next setw() operates on the value immediately following the setw() .

Lastly the setprecision() and fixed manipulators are "sticky" meaning that they stay the same until you change them, so you probably only need these statements once.

You probably should get rid of all those extra spaces in your printout and just use the setw() function properly for all the items.
Hi, thanks a lot for the reply. I used what you've said and have made some progress, 2 of the payslips are outputting as intended. However, a few of the vertical lines in the other 2 payslips are jumbled. (Those being the first and last). Would you have any idea with what's wrong? Output: http://oi64.tinypic.com/5vhw7.jpg


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

        cout  << "------------------------------------------------"  << endl;
        cout  << "|" << setw(25) << "PAYSLIP" << setw(22) << "|"  << endl;
        cout  << "| Employee number: "   << dm_employee_no << "                         |" << endl;
        cout  << "|" << setw(47) << "|"  << endl;
        cout  << "|" << setw(47) << "|"  << endl;
        cout  << "|Gross pay : "         << fixed << setprecision(2) << dm_gross << setw(28) << "|" << endl;
        cout  << "|" << setw(47) << "|"  << endl;
        cout  << "| PAYE  : "  << dm_paye  << setw(32) << "|" <<  endl;
        cout  << "| PRSI  : "  << dm_prsi  << setw(33) << "|" <<  endl;
        cout  << "| USC   : "  << dm_usc   << setw(33) << "|" <<  endl;
        cout  << "| Union : "  << dm_union << setw(33) << "|" <<  endl;
        cout  << "| VHI   : "  << dm_vhi   << setw(33) << "|" <<  endl;
        cout  << "| Netpay: "  << dm_net   << setw(32) << "|" <<  endl;
        cout  << "|" << setw(47) << "|"  << endl;
        cout  << "------------------------------------------------"  << endl;

You probably want to use setw() for your variables as well.

Topic archived. No new replies allowed.