fork download
  1. // Assignment 11 - Employee Pay Calculator using C++ Classes
  2. //
  3. // Name: Felix Henriquez
  4. //
  5. // Class: C Programming, Fall 2025
  6. //
  7. // Date: November 30, 2025
  8. //
  9. // Description: This program implements an object-oriented design using C++
  10. // to calculate employee pay, taxes, and net pay. It uses
  11. // classes to encapsulate employee data and calculations.
  12.  
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <string>
  16. #include <cstring>
  17.  
  18. using namespace std;
  19.  
  20. // Constants for tax rates and overtime
  21. const float FED_TAX_RATE = 0.25;
  22. const float OT_RATE = 1.5;
  23. const int STD_HOURS = 40;
  24.  
  25. class Employee {
  26. private:
  27. string firstName;
  28. string lastName;
  29. string taxState;
  30. string clockNumber;
  31. float wageRate;
  32. float hours;
  33. float overtimeHrs;
  34. float grossPay;
  35. float stateTax;
  36. float fedTax;
  37. float netPay;
  38.  
  39. public:
  40. // Constructor
  41. Employee() : wageRate(0), hours(0), overtimeHrs(0), grossPay(0),
  42. stateTax(0), fedTax(0), netPay(0) {}
  43.  
  44. // Member function to set employee data from provided values
  45. void setEmployeeData(string fname, string lname, string state,
  46. string clock, float wage, float hrs) {
  47. firstName = fname;
  48. lastName = lname;
  49. taxState = state;
  50. clockNumber = clock;
  51. wageRate = wage;
  52. hours = hrs;
  53. }
  54.  
  55. // Member function to calculate overtime hours
  56. void calculateOvertimeHrs() {
  57. overtimeHrs = (hours > STD_HOURS) ? (hours - STD_HOURS) : 0.0;
  58. }
  59.  
  60. // Member function to calculate gross pay
  61. void calculateGrossPay() {
  62. float regularHrs = hours - overtimeHrs;
  63. grossPay = (regularHrs * wageRate) + (overtimeHrs * wageRate * OT_RATE);
  64. }
  65.  
  66. // Member function to calculate state tax
  67. void calculateStateTax() {
  68. if (taxState == "MA") {
  69. stateTax = grossPay * 0.05;
  70. } else if (taxState == "VT") {
  71. stateTax = grossPay * 0.06;
  72. } else if (taxState == "NH") {
  73. stateTax = 0.00;
  74. } else if (taxState == "CA") {
  75. stateTax = grossPay * 0.07;
  76. } else if (taxState == "NY") {
  77. stateTax = grossPay * 0.08;
  78. } else {
  79. stateTax = 0.00;
  80. }
  81. }
  82.  
  83. // TODO: Implement calculateFedTax member function
  84. void calculateFedTax() {
  85. fedTax = grossPay * FED_TAX_RATE;
  86. }
  87.  
  88. // TODO: Implement calculateNetPay member function
  89. void calculateNetPay() {
  90. netPay = grossPay - stateTax - fedTax;
  91. }
  92.  
  93. // Member function to display employee data
  94. void displayEmployeeData() const {
  95. cout << "\n *** Entered Details are *** \n\n";
  96. cout << " First Name: " << firstName << endl;
  97. cout << " Last Name: " << lastName << endl;
  98. cout << " Tax State: " << taxState << endl;
  99. cout << " Clock Number: " << clockNumber << endl;
  100. cout << fixed << setprecision(2);
  101. cout << " Wage Rate: " << wageRate << endl;
  102. cout << " Hours: " << hours << endl;
  103. }
  104.  
  105. // Member function to display calculated values
  106. void displayCalculatedValues() const {
  107. cout << "\n *** Calculated Values are *** \n\n";
  108. cout << fixed << setprecision(2);
  109. cout << " Overtime Hours : " << overtimeHrs << endl;
  110. cout << " Gross Pay : $" << grossPay << endl;
  111. cout << " State Tax : $" << stateTax << endl;
  112. cout << " Federal Tax : $" << fedTax << endl;
  113. cout << " Net Pay : $" << netPay << endl;
  114. }
  115.  
  116. // Advanced display function for formatted output
  117. void displayFormatted() const {
  118. // Format clock number with leading zeros if needed
  119. string formattedClock = clockNumber;
  120. if (formattedClock.length() < 6) {
  121. formattedClock = string(6 - formattedClock.length(), '0') + formattedClock;
  122. }
  123.  
  124. // Format the full name
  125. string fullName = firstName + " " + lastName;
  126. if (fullName.length() > 19) {
  127. fullName = fullName.substr(0, 19);
  128. }
  129.  
  130. cout << left << setw(19) << fullName
  131. << right << setw(4) << taxState << " "
  132. << setw(6) << formattedClock << " "
  133. << setw(6) << fixed << setprecision(2) << wageRate << " "
  134. << setw(5) << setprecision(1) << hours << " "
  135. << setw(4) << setprecision(1) << overtimeHrs << " "
  136. << setw(7) << setprecision(2) << grossPay << " "
  137. << setw(6) << setprecision(2) << stateTax << " "
  138. << setw(6) << setprecision(2) << fedTax << " "
  139. << setw(8) << setprecision(2) << netPay << endl;
  140. }
  141. };
  142.  
  143. // Function to display advanced formatted output
  144. void displayAdvancedOutput(const Employee employees[], int count) {
  145. cout << "\n\n*** Pay Calculator ***\n\n";
  146. cout << "---------------------------------------------------------------------------------" << endl;
  147. cout << "Name Tax Clock# Wage Hours OT Gross State Fed Net" << endl;
  148. cout << " State Pay Tax Tax Pay" << endl;
  149. cout << "---------------------------------------------------------------------------------" << endl;
  150.  
  151. for (int i = 0; i < count; i++) {
  152. employees[i].displayFormatted();
  153. }
  154.  
  155. cout << "---------------------------------------------------------------------------------" << endl;
  156. }
  157.  
  158. int main() {
  159. const int NUM_EMPLOYEES = 5;
  160. Employee employees[NUM_EMPLOYEES];
  161.  
  162. cout << fixed << setprecision(2);
  163.  
  164. // Pre-defined employee data for Ideone.com (no interactive input)
  165. string first_names[] = {"Connie", "Mary", "Frank", "Jeff", "Anton"};
  166. string last_names[] = {"Cobol", "Apl", "Fortran", "Ada", "Pascal"};
  167. string tax_states[] = {"MA", "NH", "VT", "NY", "CA"};
  168. string clock_numbers[] = {"98401", "526488", "765349", "34645", "127615"};
  169. float wage_rates[] = {10.60, 9.75, 10.50, 12.25, 8.35};
  170. float hours[] = {51.0, 42.5, 37.0, 45.0, 40.0};
  171.  
  172. // Process each employee
  173. for (int i = 0; i < NUM_EMPLOYEES; i++) {
  174. // Set employee data from pre-defined arrays
  175. employees[i].setEmployeeData(first_names[i], last_names[i], tax_states[i],
  176. clock_numbers[i], wage_rates[i], hours[i]);
  177.  
  178. // Calculate all values
  179. employees[i].calculateOvertimeHrs();
  180. employees[i].calculateGrossPay();
  181. employees[i].calculateStateTax();
  182. employees[i].calculateFedTax(); // TODO: Call the federal tax calculation
  183. employees[i].calculateNetPay(); // TODO: Call the net pay calculation
  184.  
  185. // Display results
  186. employees[i].displayEmployeeData();
  187. employees[i].displayCalculatedValues();
  188. }
  189.  
  190. // Display advanced formatted output
  191. displayAdvancedOutput(employees, NUM_EMPLOYEES);
  192.  
  193. return 0;
  194. }
Success #stdin #stdout 0.01s 5320KB
stdin
Connie
Cobol
MA
98401
10.60
51.0
Mary
Apl
NH
526488
9.75
42.5
Frank
Fortran
VT
765349
10.50
37.0
Jeff
Ada
NY
34645
12.25
45
Anton
Pascal
CA
127615
8.35
40.0
stdout
 *** Entered Details are *** 

 First Name: Connie
 Last Name: Cobol
 Tax State: MA
 Clock Number: 98401
 Wage Rate: 10.60
 Hours: 51.00

 *** Calculated Values are *** 

 Overtime Hours : 11.00
 Gross Pay : $598.90
 State Tax : $29.95
 Federal Tax : $149.73
 Net Pay : $419.23

 *** Entered Details are *** 

 First Name: Mary
 Last Name: Apl
 Tax State: NH
 Clock Number: 526488
 Wage Rate: 9.75
 Hours: 42.50

 *** Calculated Values are *** 

 Overtime Hours : 2.50
 Gross Pay : $426.56
 State Tax : $0.00
 Federal Tax : $106.64
 Net Pay : $319.92

 *** Entered Details are *** 

 First Name: Frank
 Last Name: Fortran
 Tax State: VT
 Clock Number: 765349
 Wage Rate: 10.50
 Hours: 37.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross Pay : $388.50
 State Tax : $23.31
 Federal Tax : $97.12
 Net Pay : $268.07

 *** Entered Details are *** 

 First Name: Jeff
 Last Name: Ada
 Tax State: NY
 Clock Number: 34645
 Wage Rate: 12.25
 Hours: 45.00

 *** Calculated Values are *** 

 Overtime Hours : 5.00
 Gross Pay : $581.88
 State Tax : $46.55
 Federal Tax : $145.47
 Net Pay : $389.86

 *** Entered Details are *** 

 First Name: Anton
 Last Name: Pascal
 Tax State: CA
 Clock Number: 127615
 Wage Rate: 8.35
 Hours: 40.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross Pay : $334.00
 State Tax : $23.38
 Federal Tax : $83.50
 Net Pay : $227.12


*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock#  Wage   Hours  OT   Gross   State  Fed      Net
                    State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401   10.60   51.0  11.0   598.90   29.95  149.73    419.23
Mary Apl             NH  526488    9.75   42.5   2.5   426.56    0.00  106.64    319.92
Frank Fortran        VT  765349   10.50   37.0   0.0   388.50   23.31   97.12    268.07
Jeff Ada             NY  034645   12.25   45.0   5.0   581.88   46.55  145.47    389.86
Anton Pascal         CA  127615    8.35   40.0   0.0   334.00   23.38   83.50    227.12
---------------------------------------------------------------------------------