Computer Science 1620

81
Computer Science 1620 Formatting

description

Computer Science 1620. Formatting. Suppose you work for the HR dept. of a company you wish to write a program to show their earnings per month Details: 20% of Salary is deducted for tax 5% is deducted for CPP 2% is deducted for EI 8% is deducted for Pension - PowerPoint PPT Presentation

Transcript of Computer Science 1620

Page 1: Computer Science 1620

Computer Science 1620

Formatting

Page 2: Computer Science 1620

Suppose you work for the HR dept. of a company you wish to write a program to show their earnings per

month Details:

20% of Salary is deducted for tax 5% is deducted for CPP 2% is deducted for EI 8% is deducted for Pension 35.00 is deducted for Health Care Employer pays 85.00 towards Health Care

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

these are matchedby the employer

Page 3: Computer Science 1620

Earnings Deductions

Description Amount Description Employee Employer

Salary 3000 Tax 600

CPP 150 150

EI 60 60

Pension 240 240

Health Plan 35 85

Total 3000 Total 1085 535

Net Pay 1915

Page 4: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

#include <iostream> using namespace std;

int main() {

return 0;}

Page 5: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

#include <iostream> using namespace std;

int main() {

return 0;}

Page 6: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

#include <iostream> using namespace std;

int main() {

float salary; cout << "Salary: "; cin >> salary;

return 0;}

Page 7: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

#include <iostream> using namespace std;

int main() {

float salary; cout << "Salary: "; cin >> salary;

return 0;}

Page 8: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

#include <iostream> using namespace std;

int main() {

float salary; cout << "Salary: "; cin >> salary;

return 0;}

Page 9: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

#include <iostream> using namespace std;

int main() {

float salary; cout << "Salary: "; cin >> salary;

return 0;}

Page 10: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

#include <iostream> using namespace std;

int main() {

float salary; cout << "Salary: "; cin >> salary;

float tax = salary * 0.2; float cpp = salary * 0.05; float ei = salary * 0.02; float pension = salary * 0.08; float employee_d = tax + cpp + ei + pension + 35.00; float employer_d = tax + cpp + ei + pension + 85.00;

return 0;}

20% of Salary is deducted for tax5% is deducted for CPP2% is deducted for EI8% is deducted for Pension35.00 is deducted for Health CareEmployer pays 85.00 towards Health Care

Page 11: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

#include <iostream> using namespace std;

int main() {

float salary; cout << "Salary: "; cin >> salary;

float tax = salary * 0.2; float cpp = salary * 0.05; float ei = salary * 0.02; float pension = salary * 0.08; float employee_d = tax + cpp + ei + pension + 35.00; float employer_d = tax + cpp + ei + pension + 85.00;

return 0;}

20% of Salary is deducted for tax5% is deducted for CPP2% is deducted for EI8% is deducted for Pension35.00 is deducted for Health CareEmployer pays 85.00 towards Health Care

Page 12: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

#include <iostream> using namespace std;

int main() {

float salary; cout << "Salary: "; cin >> salary;

float tax = salary * 0.2; float cpp = salary * 0.05; float ei = salary * 0.02; float pension = salary * 0.08; // calculate total deductions for employee and employer

float your_d = tax + cpp + ei + pension + 35.00; float their_d = cpp + ei + pension + 85.00;

// continued on next slide

20% of Salary is deducted for tax5% is deducted for CPP2% is deducted for EI8% is deducted for Pension35.00 is deducted for Health CareEmployer pays 85.00 towards Health Care

Page 13: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

// continued

return 0;}

Page 14: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

// continued

return 0;}

Earnings Deductions

Description Amount Description Employee Employer

Salary 3000 Tax 600

CPP 150 150

EI 60 60

Pension 240 240

Health Plan 35 85

Total 3000 Total 1085 535

Net Pay 1915

Page 15: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

// continued

cout << "Earnings Deductions" << endl; cout << "Description Amount Description Employee Employer" << endl; cout << "Salary " << salary << " Tax " << tax << endl; cout << "CPP " << cpp << " " << cpp << endl; cout << "EI " << ei << " " << ei << endl; cout << "Pension " << pension << " " << pension << endl; cout << "Health Plan " << 35.00 << " " << 85.00 << endl; cout << "Total " << your_d << " Total " << their_d << endl; cout << "Net Pay " << salary – your_d << endl;

return 0;}

Earnings Deductions

Description Amount Description Employee Employer

Salary 3000 Tax 600

CPP 150 150

EI 60 60

Pension 240 240

Health Plan 35 85

Total 3000 Total 1085 535

Net Pay 1915

Page 16: Computer Science 1620
Page 17: Computer Science 1620

We wanted this:

Salary: 3000Earnings DeductionsDescription Amount Description Employee EmployerSalary 3000 Tax 600CPP 150 150EI 60 60Pension 240 240Health Plan 35 85Total 1085 Total 535Net Pay 1915

We got this:

Content (information) is the same Presentation (format) is different

add some lines add correct spacing between values

Earnings Deductions

Description Amount Description Employee Employer

Salary 3000 Tax 600

CPP 150 150

EI 60 60

Pension 240 240

Health Plan 35 85

Total 3000 Total 1085 535

Net Pay 1915

Page 18: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

// continued

cout << "Earnings Deductions" << endl; cout << "Description Amount Description Employee Employer" << endl; cout << "Salary " << salary << " Tax " << tax << endl; cout << "CPP " << cpp << " " << cpp << endl; cout << "EI " << ei << " " << ei << endl; cout << "Pension " << pension << " " << pension << endl; cout << "Health Plan " << 35.00 << " " << 85.00 << endl; cout << "Total " << your_d << " Total " << their_d << endl; cout << "Net Pay " << salary – your_d << endl;

return 0;

}

Add some lines!

Page 19: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

// continued

cout << "------------------------------------------------------------" << endl; cout << "Earnings Deductions" << endl; cout << "------------------------------------------------------------" << endl; cout << "Description Amount Description Employee Employer" << endl; cout << "------------------------------------------------------------" << endl; cout << "Salary " << salary << " Tax " << tax << endl; cout << "CPP " << cpp << " " << cpp << endl; cout << "EI " << ei << " " << ei << endl; cout << "Pension " << pension << " " << pension << endl; cout << "Health Plan " << 35.00 << " " << 85.00 << endl; cout << "------------------------------------------------------------" << endl; cout << "Total " << your_d << " Total " << their_d << endl; cout << "------------------------------------------------------------" << endl; cout << "Net Pay " << salary – your_d << endl;

return 0;

}

Add some lines!

Page 20: Computer Science 1620
Page 21: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

// continued

cout << "------------------------------------------------------------" << endl; cout << "Earnings Deductions" << endl; cout << "------------------------------------------------------------" << endl; cout << "Description Amount Description Employee Employer" << endl; cout << "------------------------------------------------------------" << endl; cout << "Salary " << salary << " Tax " << tax << endl; cout << "CPP " << cpp << " " << cpp << endl; cout << "EI " << ei << " " << ei << endl; cout << "Pension " << pension << " " << pension << endl; cout << "Health Plan " << 35.00 << " " << 85.00 << endl; cout << "------------------------------------------------------------" << endl; cout << "Total " << your_d << " Total " << their_d << endl; cout << "------------------------------------------------------------" << endl; cout << "Net Pay " << salary – your_d << endl;

return 0;

} Add some spaces!

Page 22: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

// continued

cout << "------------------------------------------------------------" << endl; cout << "Earnings Deductions" << endl; cout << "------------------------------------------------------------" << endl; cout << "Description Amount Description Employee Employer" << endl; cout << "------------------------------------------------------------" << endl; cout << "Salary " << salary << " Tax " << tax << endl; cout << " CPP " << cpp << " " << cpp << endl; cout << " EI " << ei << " " << ei << endl; cout << " Pension " << pension << " " << pension << endl; cout << " Health Plan " << 35.00 << " " << 85.00 << endl; cout << "------------------------------------------------------------" << endl; cout << "Total " << salary << " Total " << your_d << " " << their_d << endl; cout << "------------------------------------------------------------" << endl; cout << " Net Pay " << salary – your_d << endl;

return 0;

}Add some spaces!

Page 23: Computer Science 1620
Page 24: Computer Science 1620

Suppose employee gets a raise to $3307

Page 25: Computer Science 1620

Suppose employee gets a raise to $3307.37

Page 26: Computer Science 1620

Formatting Issues# of decimal places arbitrarysome decimals are displayed, others are notnumbers are not aligned

Page 27: Computer Science 1620

Formatting Output recall that cout has the following syntax:

cout can actually take a formatting manipulator as well:

these manipulators control what the output will look like

cout << expression

cout << expression or manipulator

Page 28: Computer Science 1620

Fixed and Scientific by default, C++ has a limit on the number of digits it

uses when displaying a floating-point number significant digits on my computer, this default is 6 (common)

what happens when the number has more than 6 digits?

#include <iostream> using namespace std; int main() { cout << 123456.7 << endl; cout << 1234567.8 << endl;

return 0;}

Page 29: Computer Science 1620

Only 6 digits are shown Number is rounded off

Page 30: Computer Science 1620

Fixed and Scientific we can instruct C++ to display all of its digits (as

many as it can store) by setting either the fixed flag or the scientific flag

if fixed is used, display numbers in fixed format if scientific is used, display numbers in scientific notation

Syntax:

cout << fixed; // fixed format

cout << scientific; // sci. format

Page 31: Computer Science 1620

Example:

#include <iostream>#include <iomanip> using namespace std; int main() { cout << 123456.7 << endl;

cout << fixed;

cout << 123456.7 << endl;

cout << scientific; cout << 123456.7 << endl; return 0;}

Page 32: Computer Science 1620

Fixed and Scientific the statement

cout << fixed

has the effect of turning on fixed mode all floating-point numbers from that point forward

will be displayed in fixed mode until: 1) fixed mode is turned off 2) scientific mode is turned on

the same applies to scientific mode

Page 33: Computer Science 1620

Example:

#include <iostream>#include <iomanip> using namespace std; int main() { cout << fixed;

cout << 123456.7 << endl; cout << 100.0 << endl;

cout << scientific; cout << 123456.7 << endl; cout << 100.0 << endl;

return 0;}

Page 34: Computer Science 1620

Fixed and Scientific by default, both fixed and scientific are off

we will refer to this as default mode

to turn off fixed (2 ways) cout << resetiosflags(ios::fixed); cout.unsetf(ios::fixed); // textbook way

to turn off scientific: cout << resetiosflags(ios::scientific); cout.unsetf(ios::scientific); // textbook way

Page 35: Computer Science 1620

Example:

#include <iostream>#include <iomanip> using namespace std; int main() { cout << fixed;

cout << 123456.7 << endl; cout << 100.0 << endl;

cout << resetiosflags(ios::fixed);

cout << 123456.7 << endl; cout << 100.0 << endl;

return 0;}

Page 36: Computer Science 1620

setprecision by default, C++ has a limit on the number of digits it

uses when displaying a floating-point number significant digits on my computer, this default is 6 (common)

can this number be set higher, without resorting to fixed or scientific mode?

use the setprecision flag

Syntax:cout << setprecision( ); # of digits

Page 37: Computer Science 1620

Example:

#include <iostream>#include <iomanip> using namespace std; int main() {

cout << 123456.7 << endl;

cout << setprecision(7);

cout << 123456.7 << endl; return 0;

}

Page 38: Computer Science 1620

setprecision in default mode, setprecision sets the

number of digits for displaying what does it do in fixed and scientific mode?

sets the number of decimal places

Page 39: Computer Science 1620

Example:#include <iostream>#include <iomanip> using namespace std; int main() {

cout << setprecision(3);

cout << 123.45 << endl;

cout << fixed;

cout << 123.45 << endl;

cout << scientific;

cout << 123.45 << endl; return 0;

}

Page 40: Computer Science 1620

setprecisionnote that setprecision does not change

the value being stored in memory it simply affects the displayed value

Page 41: Computer Science 1620

Example:#include <iostream>#include <iomanip> using namespace std; int main() {

double x = 141.5;

cout << setprecision(4); cout << x << endl;

cout << setprecision(3); cout << x << endl;

cout << setprecision(4); cout << x << endl; return 0;

}

Page 42: Computer Science 1620

showpointby default, the number 123.0 is shown as

123 in default mode for some compilers, this is also the case in fixed

modeuse the showpoint flag to force floating point

numbers to show their decimal place

Page 43: Computer Science 1620

Example:#include <iostream>#include <iomanip> using namespace std; int main() {

cout << 123.0 << endl;

cout << showpoint; cout << 123.0 << endl; return 0;

}

Page 44: Computer Science 1620

Back to our example:suppose I want all of my numbers to be

displayed with exactly two decimal placesuse the following formatting flags:

fixed showpoint setprecision(2)

Page 45: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

// continued

cout << "------------------------------------------------------------" << endl; cout << "Earnings Deductions" << endl; cout << "------------------------------------------------------------" << endl; cout << "Description Amount Description Employee Employer" << endl; cout << "------------------------------------------------------------" << endl; cout << "Salary " << salary << " Tax " << tax << endl; cout << " CPP " << cpp << " " << cpp << endl; cout << " EI " << ei << " " << ei << endl; cout << " Pension " << pension << " " << pension << endl; cout << " Health Plan " << 35.00 << " " << 85.00 << endl; cout << "------------------------------------------------------------" << endl; cout << "Total " << salary << " Total " << your_d << " " << their_d << endl; cout << "------------------------------------------------------------" << endl; cout << " Net Pay " << salary – your_d << endl;

return 0;

}Add formatting flags!

Page 46: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

// continued

cout << fixed << showpoint << setprecision(2); cout << "------------------------------------------------------------" << endl; cout << "Earnings Deductions" << endl; cout << "------------------------------------------------------------" << endl; cout << "Description Amount Description Employee Employer" << endl; cout << "------------------------------------------------------------" << endl; cout << "Salary " << salary << " Tax " << tax << endl; cout << " CPP " << cpp << " " << cpp << endl; cout << " EI " << ei << " " << ei << endl; cout << " Pension " << pension << " " << pension << endl; cout << " Health Plan " << 35.00 << " " << 85.00 << endl; cout << "------------------------------------------------------------" << endl; cout << "Total " << salary << " Total " << your_d << " " << their_d << endl; cout << "------------------------------------------------------------" << endl; cout << " Net Pay " << salary – your_d << endl;

return 0;

}Add formatting flags!

Page 47: Computer Science 1620
Page 48: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

// continued

cout << fixed << showpoint << setprecision(2); cout << "------------------------------------------------------------" << endl; cout << "Earnings Deductions" << endl; cout << "------------------------------------------------------------" << endl; cout << "Description Amount Description Employee Employer" << endl; cout << "------------------------------------------------------------" << endl; cout << "Salary " << salary << " Tax " << tax << endl; cout << " CPP " << cpp << " " << cpp << endl; cout << " EI " << ei << " " << ei << endl; cout << " Pension " << pension << " " << pension << endl; cout << " Health Plan " << 35.00 << " " << 85.00 << endl; cout << "------------------------------------------------------------" << endl; cout << "Total " << salary << " Total " << your_d << " " << their_d << endl; cout << "------------------------------------------------------------" << endl; cout << " Net Pay " << salary – your_d << endl;

return 0;

}Adjust spacing

Page 49: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

// continued

cout << fixed << showpoint << setprecision(2); cout << "------------------------------------------------------------" << endl; cout << "Earnings Deductions" << endl; cout << "------------------------------------------------------------" << endl; cout << "Description Amount Description Employee Employer" << endl; cout << "------------------------------------------------------------" << endl; cout << "Salary " << salary << " Tax " << tax << endl; cout << " CPP " << cpp << " " << cpp << endl; cout << " EI " << ei << " " << ei << endl; cout << " Pension " << pension << " " << pension << endl; cout << " Health Plan " << 35.00 << " " << 85.00 << endl; cout << "------------------------------------------------------------" << endl; cout << "Total " << salary << " Total " << your_d << " " << their_d << endl; cout << "------------------------------------------------------------" << endl; cout << " Net Pay " << salary - your_d << endl;

return 0;

}Adjust spacing

Page 50: Computer Science 1620
Page 51: Computer Science 1620
Page 52: Computer Science 1620
Page 53: Computer Science 1620

Nice Output:

but what happens if the salary is much bigger (or smaller)

Earnings Deductions

Description Amount Description Employee Employer

Salary 3000 Tax 600

CPP 150 150

EI 60 60

Pension 240 240

Health Plan 35 85

Total 3000 Total 1085 535

Net Pay 1915

Page 54: Computer Science 1620
Page 55: Computer Science 1620
Page 56: Computer Science 1620

Our spacing worked when:salary was a four digit numberEI was a three digit number

It would be nice if:everything in each column was printed at

the same width, regardless of how many characters the actual value has

Page 57: Computer Science 1620

setwstands for set widthsets the number of characters that will be

used to display the next expressionsyntax:

cout << setw( ); # of chars

Page 58: Computer Science 1620

Example:#include <iostream>#include <iomanip> using namespace std; int main() {

cout << "|" << "Kev" << "|" << endl; cout << "|" << setw(5) << "Kev" << "|" << endl; return 0;

}

Page 59: Computer Science 1620

Kev|

What happened? cout << "|" << setw(5) << "Kev" << "|" << endl;

| |

Page 60: Computer Science 1620

Kev|

What happened? cout << "|" << setw(5) << "Kev" << "|" << endl;

| |

Page 61: Computer Science 1620

Kev|

What happened? cout << "|" << setw(5) << "Kev" << "|" << endl;

| |

This tells C++ to use 5 characters to output the nextexpression.

Page 62: Computer Science 1620

Kev|

What happened? cout << "|" << setw(5) << "Kev" << "|" << endl;

| |

This expression requires only three characters for output.

Page 63: Computer Science 1620

Kev|

What happened? cout << "|" << setw(5) << "Kev" << "|" << endl;

| |

This expression requires only three characters for output.• when the field width is bigger than the expression, C++ "pads" the expression with spaces• the padding is placed on the left hand side

• expression is padded with two spaces before output• the expression now has 5 characters

Page 64: Computer Science 1620

Kev|

What happened? cout << "|" << setw(5) << "Kev" << "|" << endl;

| |

Page 65: Computer Science 1620

setwwhat happens if the output requires more

characters than setw gives it?

cout << setw(5) << "Computer Science" << endl;

• the field width is always set at least as big as the output

Page 66: Computer Science 1620

Noteunlike setprecision, fixed, scientific, and

showpoint, setw only applies to the next expression (not all expressions hereafter)

cout << setw(5) << "Kev" << "Kev" << endl;

displayed with 5 characters (2 pad spaces)

displayed with 3 characters (no padding)

Page 67: Computer Science 1620

Back to our previous example: we can break up the table into 6 columns each column has a specific width

-------------------------------------------------------------Earnings Deductions-------------------------------------------------------------Description Amount Description Employee Employer-------------------------------------------------------------Salary 3000.00 Tax 600.00 CPP 150.00 150.00 EI 60.00 60.00 Pension 240.00 240.00 Health Plan 35.00 85.00-------------------------------------------------------------Total 3000.00 Total 1085.00 535.00------------------------------------------------------------- Net Pay 1915.00

814 5 14 10 10

Page 68: Computer Science 1620

When outputting our data, we will include a setw for each expression, depending on which column it is in for the first column:

cout << setw(14) << "Salary" for the second column:

cout << setw(8) << salary; etc

What do we do with a blank field entry? cout << setw(5) << ""; // output 5 blank spaces

Page 69: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Earnings" << setw(8) << "" << setw(5) << "" << setw(14) << "Deductions" << endl; cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Description" << setw(8) << "Amount" << setw(5) << "" << setw(14) << "Description" << setw(10) << "Employee" << setw(10) << "Employer" << endl; cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Salary" << setw(8) << salary << setw(5) << "" << setw(14) << "Tax" << setw(10) << tax << endl; cout << setw(14) << "" << setw(8) << "" << setw(5) << "" << setw(14) << "CPP" << setw(10) << cpp << setw(10) << cpp << endl; cout << setw(14) << "" << setw(8) << "" << setw(5) << "" << setw(14) << "EI" << setw(10) << ei << setw(10) << ei << endl; cout << setw(14) << "" << setw(8) << "" << setw(5) << "" << setw(14) << "Pension" << setw(10) << pension << setw(10) << pension << endl; cout << setw(14) << "" << setw(8) << "" << setw(5) << "" << setw(14) << "Health Plan" << setw(10) << 35.00 << setw(10) << 85.00 << endl;

cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Total" << setw(8) << salary << setw(5) << "" << setw(14) << "Total" << setw(10) << your_d << setw(10) << their_d << endl; cout << "-------------------------------------------------------------" << endl;

cout << setw(14) << "" << setw(8) << "" << setw(5) << "" << setw(14) << "" << setw(10) << "Net Pay" << setw(10) << salary - your_d << endl; return 0;

}

Page 70: Computer Science 1620
Page 71: Computer Science 1620

Note:we can concatenate consecutive blanks into

one long blank instead of:

cout << setw(14) << "" << setw(8) << "";

we could write: cout << setw(22) << "";

Page 72: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Earnings" << setw(8) << "" << setw(5) << "" << setw(14) << "Deductions" << endl; cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Description" << setw(8) << "Amount" << setw(5) << "" << setw(14) << "Description" << setw(10) << "Employee" << setw(10) << "Employer" << endl; cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Salary" << setw(8) << salary << setw(5) << "" << setw(14) << "Tax" << setw(10) << tax << endl; cout << setw(14) << "" << setw(8) << "" << setw(5) << "" << setw(14) << "CPP" << setw(10) << cpp << setw(10) << cpp << endl; cout << setw(14) << "" << setw(8) << "" << setw(5) << "" << setw(14) << "EI" << setw(10) << ei << setw(10) << ei << endl; cout << setw(14) << "" << setw(8) << "" << setw(5) << "" << setw(14) << "Pension" << setw(10) << pension << setw(10) << pension << endl; cout << setw(14) << "" << setw(8) << "" << setw(5) << "" << setw(14) << "Health Plan" << setw(10) << 35.00 << setw(10) << 85.00 << endl;

cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Total" << setw(8) << salary << setw(5) << "" << setw(14) << "Total" << setw(10) << your_d << setw(10) << their_d << endl; cout << "-------------------------------------------------------------" << endl;

cout << setw(14) << "" << setw(8) << "" << setw(5) << "" << setw(14) << "" << setw(10) << "Net Pay" << setw(10) << salary - your_d << endl; return 0;

}

Page 73: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Earnings" << setw(13) << "" << setw(14) << "Deductions" << endl; cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Description" << setw(8) << "Amount" << setw(5) << "" << setw(14) << "Description" << setw(10) << "Employee" << setw(10) << "Employer" << endl; cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Salary" << setw(8) << salary << setw(5) << "" << setw(14) << "Tax" << setw(10) << tax << endl; cout << setw(27) << "" << setw(14) << "CPP" << setw(10) << cpp << setw(10) << cpp << endl; cout << setw(27) << "" << setw(14) << "EI" << setw(10) << ei << setw(10) << ei << endl;

cout << setw(27) << "" << setw(14) << "Pension" << setw(10) << pension << setw(10) << pension << endl; cout << setw(27) << "" << setw(14) << "Health Plan" << setw(10) << 35.00 << setw(10) << 85.00 << endl;

cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Total" << setw(8) << salary << setw(5) << "" << setw(14) << "Total" << setw(10) << your_d << setw(10) << their_d << endl; cout << "-------------------------------------------------------------" << endl;

cout << setw(41) << "" << setw(10) << "Net Pay" << setw(10) << salary - your_d << endl; return 0;

}

Page 74: Computer Science 1620

We still have a problem:setw automatically pads on the leftnow, our descriptions are right aligned

Page 75: Computer Science 1620

left and rightwhen left is used, padding spaces are put

on the right (the field is left-aligned)when right is used, padding spaces are put

on the left (the field is right aligned)note that left and right are not one-time use

when left is used, everything is left-aligned until otherwise specified (same for right)

Page 76: Computer Science 1620

Back to our previous example: column 1 and 4 are left aligned column 2, 5, and 6 are right aligned

-------------------------------------------------------------Earnings Deductions-------------------------------------------------------------Description Amount Description Employee Employer-------------------------------------------------------------Salary 3000.00 Tax 600.00 CPP 150.00 150.00 EI 60.00 60.00 Pension 240.00 240.00 Health Plan 35.00 85.00-------------------------------------------------------------Total 3000.00 Total 1085.00 535.00------------------------------------------------------------- Net Pay 1915.00

Page 77: Computer Science 1620

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee

cout << "-------------------------------------------------------------" << endl; cout << left << setw(14) << "Earnings" << setw(13) << "" << setw(14) << "Deductions" << endl; cout << "-------------------------------------------------------------" << endl; cout << setw(14) << "Description" << right << setw(8) << "Amount" << setw(5) << "" << left << setw(14) << "Description" << right << setw(10) << "Employee" << setw(10) << "Employer" << endl; cout << "-------------------------------------------------------------" << endl;

cout << left << setw(14) << "Salary" << right << setw(8) << salary << setw(5) << "" << left << setw(14) << "Tax" << right << setw(10) << tax << endl; cout << left << setw(27) << "" << setw(14) << "CPP" << right << setw(10) << cpp << setw(10) << cpp << endl; cout << left << setw(27) << "" << setw(14) << "EI" << right << setw(10) << ei << setw(10) << ei << endl; cout << left << setw(27) << "" << setw(14) << "Pension" << right << setw(10) << pension << setw(10) << pension << endl; cout << left << setw(27) << "" << setw(14) << "Health Plan" << right << setw(10) << 35.00 << setw(10) << 85.00 << endl; cout << "-------------------------------------------------------------" << endl; cout << left << setw(14) << "Total" << right << setw(8) << salary << setw(5) << "" << left << setw(14) << "Total" << right << setw(10) << your_d << setw(10) << their_d << endl; cout << "-------------------------------------------------------------" << endl; cout << setw(41) << "" << setw(10) << "Net Pay" << setw(10) << salary - your_d << endl;

return 0;

}

Page 78: Computer Science 1620
Page 79: Computer Science 1620
Page 80: Computer Science 1620
Page 81: Computer Science 1620

Format: SummaryManipulator Description One-

time use

fixed Sets output mode to fixed, all floating-point numbers displayed in fixed format

No

scientific Sets output mode to fixed, all floating-point numbers displayed in scientific notation

No

setprecision(n) In default mode, sets number of digits displayed. In fixed and scientific mode, sets number of decimal places displayed.

No

setw(n) Sets the width, in characters, of the next field to be displayed. Pads extra space with the space key.

Yes

left When setw is used, tells C++ to put extra spaces on the right side of the expression.

No

right When setw is used, tells C++ to put extra spaces on the left side of the expression.

No