Computer Science 1620 - PowerPoint PPT Presentation

About This Presentation
Title:

Computer Science 1620

Description:

Computer Science 1620 Formatting – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 82
Provided by: Kevi1245
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 1620


1
Computer Science 1620
  • Formatting

2
  • 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 matched by the employer
3
Earnings Earnings Deductions Deductions 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
4
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

include ltiostreamgt using namespace std int
main() return 0
5
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

include ltiostreamgt using namespace std int
main() return 0
6
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

include ltiostreamgt using namespace std int
main() float salary cout ltlt "Salary
" cin gtgt salary return
0
7
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

include ltiostreamgt using namespace std int
main() float salary cout ltlt "Salary
" cin gtgt salary return
0
8
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

include ltiostreamgt using namespace std int
main() float salary cout ltlt "Salary
" cin gtgt salary return
0
9
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

include ltiostreamgt using namespace std int
main() float salary cout ltlt "Salary
" cin gtgt salary return
0
10
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

include ltiostreamgt using namespace std int
main() float salary cout ltlt "Salary
" cin gtgt 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 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
11
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

include ltiostreamgt using namespace std int
main() float salary cout ltlt "Salary
" cin gtgt 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 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
12
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

include ltiostreamgt using namespace std int
main() float salary cout ltlt "Salary
" cin gtgt 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 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
13
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

// continued return 0
14
  • 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 Earnings Deductions Deductions 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
15
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

// continued cout ltlt "Earnings
Deductions" ltlt endl cout ltlt "Description
Amount Description Employee Employer" ltlt endl
cout ltlt "Salary " ltlt salary ltlt " Tax " ltlt
tax ltlt endl cout ltlt "CPP " ltlt cpp
ltlt " " ltlt cpp ltlt endl cout ltlt
"EI " ltlt ei ltlt " " ltlt ei
ltlt endl cout ltlt "Pension " ltlt pension ltlt
" " ltlt pension ltlt endl cout ltlt "Health
Plan " ltlt 35.00 ltlt " " ltlt 85.00 ltlt
endl cout ltlt "Total " ltlt your_d ltlt "
Total " ltlt their_d ltlt endl cout ltlt "Net Pay "
ltlt salary your_d ltlt endl return 0
Earnings Earnings Deductions Deductions 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
16
(No Transcript)
17
We wanted this
We got this
Salary 3000 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 1085 Total 535 Net
Pay 1915
Earnings Earnings Deductions Deductions 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
  • Content (information) is the same
  • Presentation (format) is different
  • add some lines
  • add correct spacing between values

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

// continued cout ltlt "Earnings Deductions"
ltlt endl cout ltlt "Description Amount
Description Employee Employer" ltlt endl cout ltlt
"Salary " ltlt salary ltlt " Tax " ltlt tax
ltlt endl cout ltlt "CPP " ltlt cpp ltlt
" " ltlt cpp ltlt endl cout ltlt "EI "
ltlt ei ltlt " " ltlt ei ltlt
endl cout ltlt "Pension " ltlt pension ltlt " "
ltlt pension ltlt endl cout ltlt "Health Plan
" ltlt 35.00 ltlt " " ltlt 85.00 ltlt endl
cout ltlt "Total " ltlt your_d ltlt " Total " ltlt
their_d ltlt endl cout ltlt "Net Pay " ltlt
salary your_d ltlt endl return
0
Add some lines!
19
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

// continued cout ltlt "------------------------
------------------------------------" ltlt endl
cout ltlt "Earnings Deductions" ltlt endl cout ltlt
"-------------------------------------------------
-----------" ltlt endl cout ltlt "Description
Amount Description Employee Employer" ltlt endl
cout ltlt "-----------------------------------------
-------------------" ltlt endl cout ltlt "Salary "
ltlt salary ltlt " Tax " ltlt tax ltlt
endl cout ltlt "CPP " ltlt cpp ltlt " "
ltlt cpp ltlt endl cout ltlt "EI "
ltlt ei ltlt " " ltlt ei ltlt endl
cout ltlt "Pension " ltlt pension ltlt " " ltlt
pension ltlt endl cout ltlt "Health Plan " ltlt
35.00 ltlt " " ltlt 85.00 ltlt endl cout
ltlt "----------------------------------------------
--------------" ltlt endl cout ltlt "Total "
ltlt your_d ltlt " Total " ltlt their_d ltlt endl
cout ltlt "-----------------------------------------
-------------------" ltlt endl cout ltlt "Net Pay
" ltlt salary your_d ltlt endl
return 0
Add some lines!
20
(No Transcript)
21
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

// continued cout ltlt "------------------------
------------------------------------" ltlt endl
cout ltlt "Earnings Deductions" ltlt endl cout ltlt
"-------------------------------------------------
-----------" ltlt endl cout ltlt "Description
Amount Description Employee Employer" ltlt endl
cout ltlt "-----------------------------------------
-------------------" ltlt endl cout ltlt "Salary "
ltlt salary ltlt " Tax " ltlt tax ltlt
endl cout ltlt "CPP " ltlt cpp ltlt " "
ltlt cpp ltlt endl cout ltlt "EI "
ltlt ei ltlt " " ltlt ei ltlt endl
cout ltlt "Pension " ltlt pension ltlt " " ltlt
pension ltlt endl cout ltlt "Health Plan " ltlt
35.00 ltlt " " ltlt 85.00 ltlt endl cout
ltlt "----------------------------------------------
--------------" ltlt endl cout ltlt "Total "
ltlt your_d ltlt " Total " ltlt their_d ltlt endl
cout ltlt "-----------------------------------------
-------------------" ltlt endl cout ltlt "Net Pay
" ltlt salary your_d ltlt endl
return 0
Add some spaces!
22
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

// continued cout ltlt "------------------------
------------------------------------" ltlt endl
cout ltlt "Earnings Deductions"
ltlt endl cout ltlt "------------------------------
------------------------------" ltlt endl cout
ltlt "Description Amount Description
Employee Employer" ltlt endl cout ltlt
"-------------------------------------------------
-----------" ltlt endl cout ltlt "Salary
" ltlt salary ltlt " Tax "
ltlt tax ltlt endl cout ltlt "
CPP " ltlt cpp ltlt "
" ltlt cpp ltlt endl cout ltlt "
EI " ltlt ei
ltlt " " ltlt ei ltlt endl cout ltlt
" Pension "
ltlt pension ltlt " " ltlt pension ltlt
endl cout ltlt "
Health Plan " ltlt 35.00 ltlt " "
ltlt 85.00 ltlt endl cout ltlt
"-------------------------------------------------
-----------" ltlt endl cout ltlt "Total
" ltlt salary ltlt " Total "
ltlt your_d ltlt " " ltlt their_d ltlt endl
cout ltlt "-----------------------------------------
-------------------" ltlt endl cout ltlt "
Net Pay "
ltlt salary your_d ltlt endl return
0
Add some spaces!
23
(No Transcript)
24
  • Suppose employee gets a raise to 3307

25
  • Suppose employee gets a raise to 3307.37

26
  • Formatting Issues
  • of decimal places arbitrary
  • some decimals are displayed, others are not
  • numbers are not aligned

27
  • 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 ltlt
expression
cout ltlt
expression or manipulator
28
  • 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 ltiostreamgt using namespace std int
main() cout ltlt 123456.7 ltlt endl cout ltlt
1234567.8 ltlt endl return 0
29
  • Only 6 digits are shown
  • Number is rounded off

30
  • 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 ltlt
fixed // fixed format
cout ltlt
scientific // sci. format
31
  • Example

include ltiostreamgt include ltiomanipgt using
namespace std int main() cout ltlt
123456.7 ltlt endl cout ltlt fixed cout ltlt
123456.7 ltlt endl cout ltlt scientific
cout ltlt 123456.7 ltlt endl return 0
32
  • Fixed and Scientific
  • the statement
  • cout ltlt 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

33
  • Example

include ltiostreamgt include ltiomanipgt using
namespace std int main() cout ltlt
fixed cout ltlt 123456.7 ltlt endl cout ltlt
100.0 ltlt endl cout ltlt scientific cout
ltlt 123456.7 ltlt endl cout ltlt 100.0 ltlt endl
return 0
34
  • 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 ltlt resetiosflags(iosfixed)
  • cout.unsetf(iosfixed) // textbook way
  • to turn off scientific
  • cout ltlt resetiosflags(iosscientific)
  • cout.unsetf(iosscientific) // textbook way

35
  • Example

include ltiostreamgt include ltiomanipgt using
namespace std int main() cout ltlt
fixed cout ltlt 123456.7 ltlt endl cout ltlt
100.0 ltlt endl cout ltlt resetiosflags(iosfixed
) cout ltlt 123456.7 ltlt endl cout ltlt 100.0
ltlt endl return 0
36
  • 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 ltlt setprecision( )
of digits
37
  • Example

include ltiostreamgt include ltiomanipgt using
namespace std int main() cout ltlt
123456.7 ltlt endl cout ltlt setprecision(7)
cout ltlt 123456.7 ltlt endl return 0
38
  • 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

39
  • Example

include ltiostreamgt include ltiomanipgt using
namespace std int main() cout ltlt
setprecision(3) cout ltlt 123.45 ltlt endl
cout ltlt fixed cout ltlt 123.45 ltlt endl cout
ltlt scientific cout ltlt 123.45 ltlt endl
return 0
40
  • setprecision
  • note that setprecision does not change the value
    being stored in memory
  • it simply affects the displayed value

41
  • Example

include ltiostreamgt include ltiomanipgt using
namespace std int main() double x
141.5 cout ltlt setprecision(4) cout ltlt x ltlt
endl cout ltlt setprecision(3) cout ltlt x ltlt
endl cout ltlt setprecision(4) cout ltlt x ltlt
endl return 0
42
  • showpoint
  • by default, the number 123.0 is shown as 123 in
    default mode
  • for some compilers, this is also the case in
    fixed mode
  • use the showpoint flag to force floating point
    numbers to show their decimal place

43
  • Example

include ltiostreamgt include ltiomanipgt using
namespace std int main() cout ltlt 123.0
ltlt endl cout ltlt showpoint cout ltlt 123.0
ltlt endl return 0
44
  • Back to our example
  • suppose I want all of my numbers to be displayed
    with exactly two decimal places
  • use the following formatting flags
  • fixed
  • showpoint
  • setprecision(2)

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

// continued cout ltlt "------------------------
------------------------------------" ltlt endl
cout ltlt "Earnings Deductions"
ltlt endl cout ltlt "------------------------------
------------------------------" ltlt endl cout
ltlt "Description Amount Description
Employee Employer" ltlt endl cout ltlt
"-------------------------------------------------
-----------" ltlt endl cout ltlt "Salary
" ltlt salary ltlt " Tax "
ltlt tax ltlt endl cout ltlt "
CPP " ltlt cpp ltlt "
" ltlt cpp ltlt endl cout ltlt "
EI " ltlt ei
ltlt " " ltlt ei ltlt endl cout ltlt
" Pension "
ltlt pension ltlt " " ltlt pension ltlt
endl cout ltlt "
Health Plan " ltlt 35.00 ltlt " "
ltlt 85.00 ltlt endl cout ltlt
"-------------------------------------------------
-----------" ltlt endl cout ltlt "Total
" ltlt salary ltlt " Total "
ltlt your_d ltlt " " ltlt their_d ltlt endl
cout ltlt "-----------------------------------------
-------------------" ltlt endl cout ltlt "
Net Pay "
ltlt salary your_d ltlt endl return
0
Add formatting flags!
46
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

// continued cout ltlt fixed ltlt showpoint ltlt
setprecision(2) cout ltlt "----------------------
--------------------------------------" ltlt endl
cout ltlt "Earnings
Deductions" ltlt endl cout ltlt "------------------
------------------------------------------" ltlt
endl cout ltlt "Description Amount
Description Employee Employer" ltlt endl
cout ltlt "-----------------------------------------
-------------------" ltlt endl cout ltlt "Salary
" ltlt salary ltlt " Tax
" ltlt tax ltlt endl cout ltlt "
CPP " ltlt cpp
ltlt " " ltlt cpp ltlt endl cout ltlt
" EI "
ltlt ei ltlt " " ltlt ei ltlt endl
cout ltlt " Pension
" ltlt pension ltlt " " ltlt
pension ltlt endl cout ltlt "
Health Plan " ltlt 35.00 ltlt "
" ltlt 85.00 ltlt endl cout ltlt
"-------------------------------------------------
-----------" ltlt endl cout ltlt "Total
" ltlt salary ltlt " Total "
ltlt your_d ltlt " " ltlt their_d ltlt endl cout
ltlt "----------------------------------------------
--------------" ltlt endl cout ltlt "
Net Pay "
ltlt salary your_d ltlt endl return 0
Add formatting flags!
47
(No Transcript)
48
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

// continued cout ltlt fixed ltlt showpoint ltlt
setprecision(2) cout ltlt "----------------------
--------------------------------------" ltlt endl
cout ltlt "Earnings
Deductions" ltlt endl cout ltlt "------------------
------------------------------------------" ltlt
endl cout ltlt "Description Amount
Description Employee Employer" ltlt endl
cout ltlt "-----------------------------------------
-------------------" ltlt endl cout ltlt "Salary
" ltlt salary ltlt " Tax
" ltlt tax ltlt endl cout ltlt "
CPP " ltlt cpp
ltlt " " ltlt cpp ltlt endl cout ltlt
" EI "
ltlt ei ltlt " " ltlt ei ltlt endl
cout ltlt " Pension
" ltlt pension ltlt " " ltlt
pension ltlt endl cout ltlt "
Health Plan " ltlt 35.00 ltlt "
" ltlt 85.00 ltlt endl cout ltlt
"-------------------------------------------------
-----------" ltlt endl cout ltlt "Total
" ltlt salary ltlt " Total "
ltlt your_d ltlt " " ltlt their_d ltlt endl cout
ltlt "----------------------------------------------
--------------" ltlt endl cout ltlt "
Net Pay "
ltlt salary your_d ltlt endl return 0
Adjust spacing
49
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

// continued cout ltlt fixed ltlt showpoint ltlt
setprecision(2) cout ltlt "----------------------
--------------------------------------" ltlt endl
cout ltlt "Earnings
Deductions" ltlt endl cout ltlt "------------------
------------------------------------------" ltlt
endl cout ltlt "Description Amount
Description Employee Employer" ltlt endl
cout ltlt "-----------------------------------------
-------------------" ltlt endl cout ltlt "Salary
" ltlt salary ltlt " Tax "
ltlt tax ltlt endl cout ltlt "
CPP " ltlt cpp ltlt "
" ltlt cpp ltlt endl cout ltlt "
EI " ltlt ei ltlt "
" ltlt ei ltlt endl cout ltlt "
Pension " ltlt pension ltlt
" " ltlt pension ltlt endl cout ltlt "
Health Plan " ltlt
35.00 ltlt " " ltlt 85.00 ltlt endl
cout ltlt "-----------------------------------------
-------------------" ltlt endl cout ltlt "Total
" ltlt salary ltlt " Total "
ltlt your_d ltlt " " ltlt their_d ltlt endl cout
ltlt "----------------------------------------------
--------------" ltlt endl cout ltlt "
Net Pay "
ltlt salary - your_d ltlt endl return 0
Adjust spacing
50
(No Transcript)
51
(No Transcript)
52
(No Transcript)
53
  • Nice Output
  • but what happens if the salary is much bigger (or
    smaller)

Earnings Earnings Deductions Deductions 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
54
(No Transcript)
55
(No Transcript)
56
  • Our spacing worked when
  • salary was a four digit number
  • EI 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

57
  • setw
  • stands for set width
  • sets the number of characters that will be used
    to display the next expression
  • syntax

cout ltlt setw( )
of chars
58
  • Example

include ltiostreamgt include ltiomanipgt using
namespace std int main() cout ltlt "" ltlt
"Kev" ltlt "" ltlt endl cout ltlt "" ltlt setw(5)
ltlt "Kev" ltlt "" ltlt endl return 0
59
  • What happened?

cout ltlt "" ltlt setw(5) ltlt "Kev" ltlt "" ltlt endl
Kev


60
  • What happened?

cout ltlt "" ltlt setw(5) ltlt "Kev" ltlt "" ltlt endl
Kev


61
  • What happened?

cout ltlt "" ltlt setw(5) ltlt "Kev" ltlt "" ltlt endl
This tells C to use 5 characters to output the
next expression.
Kev


62
  • What happened?

cout ltlt "" ltlt setw(5) ltlt "Kev" ltlt "" ltlt endl
This expression requires only three characters
for output.
Kev


63
  • What happened?

cout ltlt "" ltlt setw(5) ltlt "Kev" ltlt "" ltlt 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

Kev

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

64
  • What happened?

cout ltlt "" ltlt setw(5) ltlt "Kev" ltlt "" ltlt endl
Kev


65
  • setw
  • what happens if the output requires more
    characters than setw gives it?

cout ltlt setw(5) ltlt "Computer Science" ltlt endl
  • the field width is always set at least as big
    as the output

66
  • Note
  • unlike setprecision, fixed, scientific, and
    showpoint, setw only applies to the next
    expression (not all expressions hereafter)

cout ltlt setw(5) ltlt "Kev" ltlt "Kev" ltlt endl
displayed with 3 characters (no padding)
displayed with 5 characters (2 pad spaces)
67
  • Back to our previous example
  • we can break up the table into 6 columns
  • each column has a specific width

8
14
5
14
10
10
--------------------------------------------------
----------- Earnings
Deductions ---------------------------------------
---------------------- Description Amount
Description Employee Employer ---------------
---------------------------------------------- Sal
ary 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
68
  • When outputting our data, we will include a setw
    for each expression, depending on which column it
    is in
  • for the first column
  • cout ltlt setw(14) ltlt "Salary"
  • for the second column
  • cout ltlt setw(8) ltlt salary
  • etc
  • What do we do with a blank field entry?
  • cout ltlt setw(5) ltlt "" // output 5 blank spaces

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

cout ltlt "---------------------------------------
----------------------" ltlt endl cout ltlt
setw(14) ltlt "Earnings" ltlt setw(8) ltlt ""
ltlt setw(5) ltlt "" ltlt setw(14) ltlt
"Deductions" ltlt endl cout ltlt
"-------------------------------------------------
------------" ltlt endl cout ltlt setw(14) ltlt
"Description" ltlt setw(8) ltlt "Amount" ltlt
setw(5) ltlt "" ltlt setw(14) ltlt
"Description" ltlt setw(10) ltlt "Employee" ltlt
setw(10) ltlt "Employer" ltlt endl cout
ltlt "----------------------------------------------
---------------" ltlt endl cout ltlt setw(14) ltlt
"Salary" ltlt setw(8) ltlt salary ltlt setw(5)
ltlt "" ltlt setw(14) ltlt "Tax" ltlt
setw(10) ltlt tax ltlt endl cout ltlt
setw(14) ltlt "" ltlt setw(8) ltlt ""
ltlt setw(5) ltlt "" ltlt setw(14) ltlt "CPP"
ltlt setw(10) ltlt cpp ltlt setw(10) ltlt cpp
ltlt endl cout ltlt setw(14) ltlt ""
ltlt setw(8) ltlt "" ltlt setw(5) ltlt ""
ltlt setw(14) ltlt "EI" ltlt setw(10) ltlt
ei ltlt setw(10) ltlt ei ltlt endl
cout ltlt setw(14) ltlt "" ltlt setw(8) ltlt
"" ltlt setw(5) ltlt "" ltlt setw(14)
ltlt "Pension" ltlt setw(10) ltlt pension ltlt
setw(10) ltlt pension ltlt endl
cout ltlt setw(14) ltlt "" ltlt setw(8) ltlt
"" ltlt setw(5) ltlt "" ltlt setw(14)
ltlt "Health Plan" ltlt setw(10) ltlt 35.00 ltlt
setw(10) ltlt 85.00 ltlt endl cout ltlt
"-------------------------------------------------
------------" ltlt endl cout ltlt setw(14) ltlt
"Total" ltlt setw(8) ltlt salary ltlt setw(5)
ltlt "" ltlt setw(14) ltlt "Total" ltlt
setw(10) ltlt your_d ltlt setw(10) ltlt their_d ltlt
endl cout ltlt "------------------------------
-------------------------------" ltlt endl cout
ltlt setw(14) ltlt "" ltlt setw(8) ltlt ""
ltlt setw(5) ltlt "" ltlt setw(14) ltlt ""
ltlt setw(10) ltlt "Net Pay" ltlt setw(10) ltlt
salary - your_d ltlt endl return 0
70
(No Transcript)
71
  • Note
  • we can concatenate consecutive blanks into one
    long blank
  • instead of
  • cout ltlt setw(14) ltlt "" ltlt setw(8) ltlt ""
  • we could write
  • cout ltlt setw(22) ltlt ""

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

cout ltlt "---------------------------------------
----------------------" ltlt endl cout ltlt
setw(14) ltlt "Earnings" ltlt setw(8) ltlt ""
ltlt setw(5) ltlt "" ltlt setw(14) ltlt
"Deductions" ltlt endl cout ltlt
"-------------------------------------------------
------------" ltlt endl cout ltlt setw(14) ltlt
"Description" ltlt setw(8) ltlt "Amount" ltlt
setw(5) ltlt "" ltlt setw(14) ltlt
"Description" ltlt setw(10) ltlt "Employee" ltlt
setw(10) ltlt "Employer" ltlt endl cout
ltlt "----------------------------------------------
---------------" ltlt endl cout ltlt setw(14) ltlt
"Salary" ltlt setw(8) ltlt salary ltlt setw(5)
ltlt "" ltlt setw(14) ltlt "Tax" ltlt
setw(10) ltlt tax ltlt endl cout ltlt
setw(14) ltlt "" ltlt setw(8) ltlt ""
ltlt setw(5) ltlt "" ltlt setw(14) ltlt "CPP"
ltlt setw(10) ltlt cpp ltlt setw(10) ltlt cpp
ltlt endl cout ltlt setw(14) ltlt ""
ltlt setw(8) ltlt "" ltlt setw(5) ltlt ""
ltlt setw(14) ltlt "EI" ltlt setw(10) ltlt
ei ltlt setw(10) ltlt ei ltlt endl
cout ltlt setw(14) ltlt "" ltlt setw(8) ltlt
"" ltlt setw(5) ltlt "" ltlt setw(14)
ltlt "Pension" ltlt setw(10) ltlt pension ltlt
setw(10) ltlt pension ltlt endl
cout ltlt setw(14) ltlt "" ltlt setw(8) ltlt
"" ltlt setw(5) ltlt "" ltlt setw(14)
ltlt "Health Plan" ltlt setw(10) ltlt 35.00 ltlt
setw(10) ltlt 85.00 ltlt endl cout ltlt
"-------------------------------------------------
------------" ltlt endl cout ltlt setw(14) ltlt
"Total" ltlt setw(8) ltlt salary ltlt setw(5)
ltlt "" ltlt setw(14) ltlt "Total" ltlt
setw(10) ltlt your_d ltlt setw(10) ltlt their_d ltlt
endl cout ltlt "------------------------------
-------------------------------" ltlt endl cout
ltlt setw(14) ltlt "" ltlt setw(8) ltlt ""
ltlt setw(5) ltlt "" ltlt setw(14) ltlt ""
ltlt setw(10) ltlt "Net Pay" ltlt setw(10) ltlt
salary - your_d ltlt endl return 0
73
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

cout ltlt "---------------------------------------
----------------------" ltlt endl cout ltlt
setw(14) ltlt "Earnings" ltlt setw(13) ltlt "" ltlt
setw(14) ltlt "Deductions" ltlt endl cout ltlt
"-------------------------------------------------
------------" ltlt endl cout ltlt setw(14) ltlt
"Description" ltlt setw(8) ltlt "Amount" ltlt
setw(5) ltlt "" ltlt setw(14) ltlt
"Description" ltlt setw(10) ltlt "Employee" ltlt
setw(10) ltlt "Employer" ltlt endl cout
ltlt "----------------------------------------------
---------------" ltlt endl cout ltlt setw(14) ltlt
"Salary" ltlt setw(8) ltlt salary ltlt setw(5)
ltlt "" ltlt setw(14) ltlt "Tax" ltlt
setw(10) ltlt tax ltlt endl cout ltlt
setw(27) ltlt "" ltlt setw(14) ltlt "CPP" ltlt setw(10)
ltlt cpp ltlt setw(10) ltlt cpp ltlt endl cout
ltlt setw(27) ltlt "" ltlt setw(14) ltlt "EI" ltlt
setw(10) ltlt ei ltlt setw(10) ltlt ei ltlt endl
cout ltlt setw(27) ltlt "" ltlt setw(14) ltlt
"Pension" ltlt setw(10) ltlt pension ltlt
setw(10) ltlt pension ltlt endl cout ltlt
setw(27) ltlt "" ltlt setw(14) ltlt "Health Plan"
ltlt setw(10) ltlt 35.00 ltlt setw(10) ltlt
85.00 ltlt endl cout ltlt "----------------------
---------------------------------------" ltlt
endl cout ltlt setw(14) ltlt "Total" ltlt
setw(8) ltlt salary ltlt setw(5) ltlt "" ltlt
setw(14) ltlt "Total" ltlt setw(10) ltlt your_d
ltlt setw(10) ltlt their_d ltlt endl cout ltlt
"-------------------------------------------------
------------" ltlt endl cout ltlt setw(41) ltlt ""
ltlt setw(10) ltlt "Net Pay" ltlt setw(10) ltlt salary -
your_d ltlt endl return 0
74
  • We still have a problem
  • setw automatically pads on the left
  • now, our descriptions are right aligned

75
  • left and right
  • when 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)

76
  • 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 ---------------
---------------------------------------------- Sal
ary 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
77
  • Write a program that takes in a salary for the
    month, calculates the deductions, and produces a
    paystub for the employee

cout ltlt "----------------------------------------
---------------------" ltlt endl cout ltlt left ltlt
setw(14) ltlt "Earnings" ltlt setw(13) ltlt ""
ltlt setw(14) ltlt "Deductions" ltlt endl
cout ltlt "-----------------------------------------
--------------------" ltlt endl cout ltlt setw(14)
ltlt "Description" ltlt right ltlt setw(8) ltlt
"Amount" ltlt setw(5) ltlt "" ltlt left ltlt
setw(14) ltlt "Description" ltlt right ltlt
setw(10) ltlt "Employee" ltlt setw(10) ltlt
"Employer" ltlt endl cout ltlt
"-------------------------------------------------
------------" ltlt endl cout ltlt left ltlt
setw(14) ltlt "Salary" ltlt right ltlt setw(8) ltlt
salary ltlt setw(5) ltlt "" ltlt left ltlt
setw(14) ltlt "Tax" ltlt right ltlt setw(10) ltlt tax
ltlt endl cout ltlt left ltlt setw(27)
ltlt "" ltlt setw(14) ltlt "CPP" ltlt right ltlt setw(10)
ltlt cpp ltlt setw(10) ltlt cpp ltlt endl
cout ltlt left ltlt setw(27) ltlt "" ltlt setw(14) ltlt
"EI" ltlt right ltlt setw(10) ltlt ei ltlt
setw(10) ltlt ei ltlt endl cout ltlt left ltlt
setw(27) ltlt "" ltlt setw(14) ltlt "Pension" ltlt right
ltlt setw(10) ltlt pension ltlt setw(10) ltlt
pension ltlt endl cout ltlt left ltlt
setw(27) ltlt "" ltlt setw(14) ltlt "Health Plan" ltlt
right ltlt setw(10) ltlt 35.00 ltlt setw(10) ltlt
85.00 ltlt endl cout ltlt "-----------------------
--------------------------------------" ltlt endl
cout ltlt left ltlt setw(14) ltlt "Total" ltlt right ltlt
setw(8) ltlt salary ltlt setw(5) ltlt "" ltlt
left ltlt setw(14) ltlt "Total" ltlt right ltlt setw(10)
ltlt your_d ltlt setw(10) ltlt their_d ltlt
endl cout ltlt "---------------------------------
----------------------------" ltlt endl cout ltlt
setw(41) ltlt "" ltlt setw(10) ltlt "Net Pay" ltlt
setw(10) ltlt salary - your_d ltlt endl return
0
78
(No Transcript)
79
(No Transcript)
80
(No Transcript)
81
  • Format Summary

Manipulator 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
Write a Comment
User Comments (0)
About PowerShow.com