fork download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. struct EmployeeData
  5. {
  6. std::string name;
  7. int hours;
  8. int rate;
  9. };
  10.  
  11. struct Salary
  12. {
  13. Salary(EmployeeData);
  14. void print(std::ostream&);
  15. private:
  16. EmployeeData employee;
  17. int gross_pay;
  18. int federal_tax;
  19. int state_tax;
  20. int net_pay;
  21. };
  22.  
  23. Salary::Salary(EmployeeData ed)
  24. : employee(ed)
  25. {
  26. if (employee.hours <= 40)
  27. {
  28. gross_pay = employee.hours * employee.rate;
  29. }
  30. else
  31. {
  32. gross_pay = (40 * employee.rate) + (((employee.hours - 40) * 3 * employee.rate) / 2);
  33. }
  34. federal_tax = gross_pay / 5;
  35. state_tax = gross_pay / 10;
  36. net_pay = gross_pay - federal_tax - state_tax;
  37. }
  38.  
  39. void Salary::print(std::ostream &out)
  40. {
  41. if (!employee.name.empty())
  42. {
  43. out << std::endl << employee.name << std::endl;
  44. }
  45. out << "Worked " << employee.hours << " hours" << std::endl
  46. << "$" << employee.rate << " per hour" << std::endl
  47. << "Gross pay is $" << gross_pay << std::endl
  48. << "Federal taxed ammount is $" << federal_tax << std::endl
  49. << "State taxed ammount is $" << state_tax << std::endl
  50. << "Net pay is $" << net_pay << std::endl;
  51. }
  52.  
  53. void queryData(EmployeeData *ed)
  54. {
  55. std::cout << "Name:" << std::endl;
  56. std::cin >> ed->name;
  57. std::cout << "Hours:" << std::endl;
  58. std::cin >> ed->hours;
  59. std::cout << "Rate:" << std::endl;
  60. std::cin >> ed->rate;
  61. }
  62.  
  63. int main(int argc, char** argv)
  64. {
  65. EmployeeData ed;
  66. if (argc == 3)
  67. {
  68. ed.hours = atoi(argv[1]);
  69. ed.rate = atoi(argv[2]);
  70. }
  71. else
  72. {
  73. queryData(&ed);
  74. }
  75. Salary salary(ed);
  76. salary.print(std::cout);
  77. }
  78.  
Success #stdin #stdout 0s 3024KB
stdin
foo
45
8
stdout
Name:
Hours:
Rate:

foo
Worked 45 hours
$8 per hour
Gross pay is $380
Federal taxed ammount is $76
State taxed ammount is $38
Net pay is $266