formatting

I need to have the output formatted to look like this

Name Hours worked Pay rate Gross Pay Social Security Income Tax Net Pay
----------------- ------------ -------- --------- --------------- ---------- -------
Jim Jones 30.0 10.00 300.00 16.50 30.00 253.50



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
  #include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
    string firstname;
    string lastname;
    double hours;
    double wage;
    
    double gross;
   
    double incometax;
    
    
    cout<< "Enter your first name and last name \n";      
    cin>> firstname>>lastname;                            
    cout<< "Enter amount of hours worked up to two decimal points. \n"; 
    cin>>hours;                                             
    cout<< "Enter your hourly wage \n";                    
    cin>>wage;                                              
    
   
    
    //statement calculates gross pay based on hours worked
    if(hours<=40){           
        gross=wage*hours;    
    }
    else{
        gross = (40*wage) + (1.5*wage);
    } 
    
    
    
    //calculates income tax
    if(gross <100){
        incometax=0;        
    }
    else if(gross >= 100.01 && gross <= 500.00)
        
    {
        incometax = gross* .1;
    }
    else{
        incometax=gross * .2;
    }  

    

     double social= gross * .055;              
    double net= gross - incometax - social;     


    cout<< "Name: "<<firstname<<lastname<<endl;
    cout<< "Hours Worked: "<<hours<<endl;
    cout<< "Pay Rate: "<<wage<<endl;
    cout<< "Gross Pay: "<<gross<<endl;
    cout<< "Social Security: "<<social<<endl;
    cout<< "Income Tax: "<<incometax<<endl;
    cout<< "Net Pay: "<<net<<endl;
    return 0;
}



Can anyone help?
You can use "\t" to put a tab between output fields or you can use setw in <iomanip> to explicitly set the width to a certain number of characters.

http://www.cplusplus.com/reference/iomanip/
Okay, now i have it so that it will look right until you change names. I can't get the numbers to stay in the same place every time I type a different name.

1
2
3
4
5
6
7
8
9
10
11
12
    cout<<"Name"<<setw(28)<<"Hours Worked"<<setw(12)<<"Pay Rate"<<setw(15)<<"Gross Pay";
    cout<<setw(20)<<"Social Security"<<setw(15)<<"Income Tax"<<setw(10)<<"Net Pay"<<endl;
 
    cout<<setfill('-')<<setw(20)<<"    "<<setw(16)<<"    "<<setw(14)<<"      "<<setw(14)<<"     "<<setw(20)<<"     "<< setw(13)<<"   "<< setw(9)<<"  "<<endl;
    

    cout<<setfill(' ')<<setprecision(2)<<fixed<<firstname <<" "<<lastname<<setw(12)<<" "<<setw(7)<<hours<<" "<<setw(14)<<wage<<" "<<setw(13)<<gross<<" "<<setw(15)<<social <<" "<<setw(18)<<incometax<<" "<<setw(11)<<net<<endl;

    
    return 0;
}
Topic archived. No new replies allowed.