Formatting an output into neat columns
Dec 14, 2015 at 1:30am UTC
Hey Everyone! First time posting...still very new to this programming game! :D
Anywho, this is a payroll program, and I want it to output into nice neat columns. I've monkeyed around a lot with the setw (as that's the only way I can think of to space thing besides the " " way. Any help would be much appreciated! This is for an assignment btw.
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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
//Develop Variables
int empId[100];
string fName[100], lName[100];
int hourWork[100], hourRate[100];
double gp[100], overhours[100], taxamt[100], overtimepay[100], netpay[100];
const double taxrate = .07;
int j = 0;
//Ask for Employee ID, First Name, Last Name, Hours Worked, and Hourly Rate in a loop (for 2)
while (j<=1)
{
cout << "Please Enter 4-Digit Employee ID." << endl;
cin >> empId[j];
cout << "Please Enter First Name." << endl;
cin >> fName[j];
cout << "Please Enter Last Name." << endl;
cin >> lName[j];
cout << "Please Enter Hours Worked." << endl;
cin >> hourWork[j];
cout << "Please Enter Hourly Rate:" << endl;
cin >> hourRate[j];
cout<<"" <<endl;
j = j + 1;
}
j = 0;
//Compute in a loop for each entered
while (j<=1)
{
if (hourWork[j]>40)
{
overtimepay[j]=overhours[j]*(hourRate[j]*1.5);
gp[j]= overtimepay[j]+ (hourWork[j]*hourRate[j]);
taxamt[j]=gp[j]*taxrate;
netpay[j]= gp[j]-taxamt[j];
}
else
{
gp[j]=hourWork[j]*hourRate[j];
taxamt[j]=gp[j]*taxrate;
netpay[j]=gp[j]-taxamt[j];
}
j=j+1;}
j = 0;
//Display company title & Column Names
cout << setw(55) << "Dr. Ebrahimi's Payroll Program" << endl;
cout << setw(45) << "106 Easy Ways" << endl;
cout << setw(52) << "Pleasantville, N.Y. 11068" << endl;
cout<<"" <<endl;
cout<<left<<setw(10)<<"|First" <<setw(10)<<"|Last" <<setw(10)<<"|H.W." <<setw(10)<<"|H.R."
<<setw(10)<<"|G.P." <<setw(10)<<"|Taxes" <<setw(10)<<"|NetPay" <<endl;
cout<<"" <<endl;
//Output calculations in tabular form
while (j<=2)
{
cout<<left<<"" <<fName[j]<<setw(10)<<"" <<lName[j]<<setw(5)<<"" <<hourWork[j]<<
setw(5)<<"" <<hourRate[j]<<setw(5)<<"" <<gp[j]<<setw(5)<<"" <<taxamt[j]<<setw(5)
<<"" <<netpay[j]<<endl;
cout<<"" <<endl;
j=j+1;
}
system ("pause" );
return 0;
}
Dec 14, 2015 at 2:39am UTC
It helps if the code is laid out in an easy-to-read manner. Sometimes splitting the output statement over multiple lines like this can make it easier to read:
1 2 3 4 5 6 7 8 9
cout << left
<< setw(10) << "|First"
<< setw(10) << "|Last"
<< setw(10) << "|H.W."
<< setw(10) << "|H.R."
<< setw(10) << "|G.P."
<< setw(10) << "|Taxes"
<< setw(10) << "|NetPay"
<< endl;
Now we can see the width of each column it is easy enough to set the detail lines of the report to a similar spacing:
1 2 3 4 5 6 7 8 9
cout << left
<< setw(10) << fName[j]
<< setw(10) << lName[j]
<< setw(10) << hourWork[j]
<< setw(10) << hourRate[j]
<< setw(10) << gp[j]
<< setw(10) << taxamt[j]
<< setw(10) << netpay[j]
<< endl;
Though you might consider this, use left-align for the names and right-align for the numbers. That will change the layout, so adjust the column headings accordingly.
1 2 3 4 5 6 7 8 9 10 11 12
cout << fixed << setprecision(2); // need once only
cout << left
<< setw(10) << fName[j]
<< setw(10) << lName[j]
<< right
<< setw(10) << hourWork[j]
<< setw(10) << hourRate[j]
<< setw(10) << gp[j]
<< setw(10) << taxamt[j]
<< setw(10) << netpay[j]
<< endl;
Last edited on Dec 14, 2015 at 2:51am UTC
Dec 14, 2015 at 3:03am UTC
Maintenance (fine tuning the format) is a lot easier if we do not have magic values in the code.
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
#include <iostream>
#include <string>
#include <iomanip>
int main()
{
// arbitrary test data, test repeats the same data thrice
std::string fname = "Ebenezer" ;
std::string lname = "Scrooge" ;
int hourWork = 23 ;
int hourRate = 5 ;
double gp = 123.48501 ;
double taxamt = 2.4392 ;
double netpay = 121.04579 ;
// values for controlling format
const int name_width = 15 ;
const int int_width = 7 ;
const int dbl_width = 12 ;
const int num_flds = 7 ;
const std::string sep = " |" ;
const int total_width = name_width*2 + int_width*2 + dbl_width*3 + sep.size() * num_flds ;
const std::string line = sep + std::string( total_width-1, '-' ) + '|' ;
std::cout << line << '\n' << sep
<< std::setw(name_width) << "first name" << sep << std::setw(name_width) << "last name" << sep
<< std::setw(int_width) << "hours" << sep << std::setw(int_width) << "rate" << sep
<< std::setw(dbl_width) << "gross pay" << sep << std::setw(dbl_width) << "tax" << sep
<< std::setw(dbl_width) << "net pay" << sep << '\n' << line << '\n' ;
for ( int i = 0 ; i < 3 ; ++i )
{
std::cout << sep << std::setw(name_width) << fname << sep << std::setw(name_width) << lname << sep
<< std::setw(int_width) << hourWork << sep << std::setw(int_width) << hourRate << sep
<< std::fixed << std::setprecision(2)
<< std::setw(dbl_width) << gp << sep << std::setw(dbl_width) << taxamt << sep
<< std::setw(dbl_width) << netpay << sep << '\n' ;
}
std::cout << line << '\n' ;
}
|---------------------------------------------------------------------------------------------|
| first name | last name | hours | rate | gross pay | tax | net pay |
|---------------------------------------------------------------------------------------------|
| Ebenezer | Scrooge | 23 | 5 | 123.49 | 2.44 | 121.05 |
| Ebenezer | Scrooge | 23 | 5 | 123.49 | 2.44 | 121.05 |
| Ebenezer | Scrooge | 23 | 5 | 123.49 | 2.44 | 121.05 |
|---------------------------------------------------------------------------------------------|
http://rextester.com/VIAUU19057
Dec 14, 2015 at 3:04am UTC
This worked perfectly. The way you aligned the << made a lot of sense for readability!
Thank you!
Topic archived. No new replies allowed.