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:

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
Numeric Types, Expressions, and Output ROBERT REAVES.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Computer Science 1620 Programming & Problem Solving.
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Chapter 2: Introduction to C++.
CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems.
CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Input, Output, and Processing
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
CPS120: Introduction to Computer Science Formatted I/O.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 CS161 Introduction to Computer Science Topic #4.
Expressions and Interactivity. 3.1 The cin Object.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 05 (Part II) Control Statements: Part II.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Chapter 3 Selection Statements
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
Chapter 3: Expressions and Interactivity.
Output Stream Formatting
Formatting Screen Input/Output
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Formatted Input, Output & File Input, Output
Let’s all Repeat Together
C++ for Engineers and Scientists Second Edition
Presentation transcript:

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 is deducted for Health Care Employer pays 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

EarningsDeductions DescriptionAmountDescriptionEmployeeEmployer Salary3000Tax600 CPP150 EI60 Pension240 Health Plan3585 Total3000Total Net Pay1915

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee #include using namespace std; int main() { return 0; }

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee #include using namespace std; int main() { return 0; }

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee #include using namespace std; int main() { float salary; cout << "Salary: "; cin >> salary; return 0; }

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee #include using namespace std; int main() { float salary; cout << "Salary: "; cin >> salary; return 0; }

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee #include using namespace std; int main() { float salary; cout << "Salary: "; cin >> salary; return 0; }

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee #include using namespace std; int main() { float salary; cout << "Salary: "; cin >> salary; return 0; }

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee #include 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 ; float employer_d = tax + cpp + ei + pension ; return 0; } 20% of Salary is deducted for tax 5% is deducted for CPP 2% is deducted for EI 8% is deducted for Pension is deducted for Health Care Employer pays towards Health Care

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee #include 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 ; float employer_d = tax + cpp + ei + pension ; return 0; } 20% of Salary is deducted for tax 5% is deducted for CPP 2% is deducted for EI 8% is deducted for Pension is deducted for Health Care Employer pays towards Health Care

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee #include 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 ; float their_d = cpp + ei + pension ; // 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 is deducted for Health Care Employer pays towards Health Care

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee // continued return 0; }

Write a program that takes in a salary for the month, calculates the deductions, and produces a paystub for the employee // continued return 0; } EarningsDeductions DescriptionAmountDescriptionEmployeeEmployer Salary3000Tax600 CPP150 EI60 Pension240 Health Plan3585 Total3000Total Net Pay1915

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 " << << " " << << endl; cout << "Total " << your_d << " Total " << their_d << endl; cout << "Net Pay " << salary – your_d << endl; return 0; } EarningsDeductions DescriptionAmountDescriptionEmployeeEmployer Salary3000Tax600 CPP150 EI60 Pension240 Health Plan3585 Total3000Total Net Pay1915

We wanted this: Salary: 3000 Earnings Deductions Description Amount Description Employee Employer Salary 3000 Tax 600 CPP EI Pension Health Plan Total 1085 Total 535 Net Pay 1915 We got this: Content (information) is the same Presentation (format) is different add some lines add correct spacing between values EarningsDeductions DescriptionAmountDescriptionEmployeeEmployer Salary3000Tax600 CPP150 EI60 Pension240 Health Plan3585 Total3000Total Net Pay1915

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 " << << " " << << endl; cout << "Total " << your_d << " Total " << their_d << endl; cout << "Net Pay " << salary – your_d << endl; return 0; } Add some lines!

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 " << << " " << << 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!

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 " << << " " << << 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!

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 " << << " " << << 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!

Suppose employee gets a raise to $3307

Suppose employee gets a raise to $

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

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

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 using namespace std; int main() { cout << << endl; cout << << endl; return 0; }

Only 6 digits are shown Number is rounded off

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

Example: #include using namespace std; int main() { cout << << endl; cout << fixed; cout << << endl; cout << scientific; cout << << endl; return 0; }

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

Example: #include using namespace std; int main() { cout << fixed; cout << << endl; cout << << endl; cout << scientific; cout << << endl; cout << << endl; return 0; }

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

Example: #include using namespace std; int main() { cout << fixed; cout << << endl; cout << << endl; cout << resetiosflags(ios::fixed); cout << << endl; cout << << endl; return 0; }

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

Example: #include using namespace std; int main() { cout << << endl; cout << setprecision(7); cout << << endl; return 0; }

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

Example: #include using namespace std; int main() { cout << setprecision(3); cout << << endl; cout << fixed; cout << << endl; cout << scientific; cout << << endl; return 0; }

setprecision note that setprecision does not change the value being stored in memory it simply affects the displayed value

Example: #include 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; }

showpoint by default, the number 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

Example: #include using namespace std; int main() { cout << << endl; cout << showpoint; cout << << endl; return 0; }

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)

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 " << << " " << << 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!

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 " << << " " << << 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!

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 " << << " " << << 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

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 " << << " " << << 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

Nice Output: but what happens if the salary is much bigger (or smaller) EarningsDeductions DescriptionAmountDescriptionEmployeeEmployer Salary3000Tax600 CPP150 EI60 Pension240 Health Plan3585 Total3000Total Net Pay1915

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

setw stands for set width sets the number of characters that will be used to display the next expression syntax: cout << setw( ); # of chars

Example: #include using namespace std; int main() { cout << "|" << "Kev" << "|" << endl; cout << "|" << setw(5) << "Kev" << "|" << endl; return 0; }

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

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

Kev| What happened? cout << "|" << setw(5) << "Kev" << "|" << endl; || This tells C++ to use 5 characters to output the next expression.

Kev| What happened? cout << "|" << setw(5) << "Kev" << "|" << endl; || This expression requires only three characters for output.

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

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

setw what 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

Note unlike 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)

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 Tax CPP EI Pension Health Plan Total Total Net Pay

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

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) << << setw(10) << << 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; }

Note: we can concatenate consecutive blanks into one long blank instead of: cout << setw(14) << "" << setw(8) << ""; we could write: cout << setw(22) << "";

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) << << setw(10) << << 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; }

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) << << setw(10) << << 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; }

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

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)

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 Tax CPP EI Pension Health Plan Total Total Net Pay

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) << << setw(10) << << 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; }

Format: Summary ManipulatorDescription One- time use fixedSets output mode to fixed, all floating-point numbers displayed in fixed format No scientificSets 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 leftWhen setw is used, tells C++ to put extra spaces on the right side of the expression. No rightWhen setw is used, tells C++ to put extra spaces on the left side of the expression. No