1 / 81

Computer Science 1620

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

ashtyn
Download Presentation

Computer Science 1620

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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; }

  4. 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; }

  5. 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; }

  6. 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; }

  7. 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; }

  8. 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; }

  9. 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 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

  10. 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 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 <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 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 // continued return 0; }

  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 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; }

  15. We got this: We wanted this: • Content (information) is the same • Presentation (format) is different • add some lines • add correct spacing between values 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

  16. 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!

  17. 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!

  18. 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!

  19. 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!

  20. Suppose employee gets a raise to $3307

  21. Suppose employee gets a raise to $3307.37

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

  23. Formatting Output • recall that cout has the following syntax: • cout can actually take a formattingmanipulator as well: • these manipulators control what the output will look like cout << expression cout << expression or manipulator

  24. 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; }

  25. Only 6 digits are shown • Number is rounded off

  26. 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

  27. 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; }

  28. 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

  29. 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; }

  30. 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

  31. 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; }

  32. 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

  33. Example: #include <iostream> #include <iomanip> using namespace std; int main() { cout << 123456.7 << endl; cout << setprecision(7); cout << 123456.7 << endl; return 0; }

  34. 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

  35. 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; }

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

  37. 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; }

  38. 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

  39. Example: #include <iostream> #include <iomanip> using namespace std; int main() { cout << 123.0 << endl; cout << showpoint; cout << 123.0 << endl; return 0; }

  40. 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)

  41. 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!

  42. 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!

  43. 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

  44. 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

More Related