In: Computer Science
C++ Programming
Enum - Structure - Array
You are asked to develop software for HR department to calculate employee’s weekly salary. The program should contain the following information about a student by declaring a struct:
Name (string of characters)
Employee ID (string of characters)
Level (ENGINEER, MANGER, DIRECTOR)
Hourly Rate (floating-point number)
Working Hours (floating-point number)
Weekly Salary (floating-point number)
Your program will read an employee data and print the information of employee’s Name, Employee ID, level, weekly salary, and gender. Program will perform to calculate each employee’s weekly salary and display all employee’s data. The program will be terminated when users enter “N” to discontinue entering information.
The calculation formula:
Over time pay = hourly rate * (working hours - 40) * 1.15.
Over time pay = hourly rate * (working hours - 40) * 1.20
Over time pay = hourly rate * (working hours - 40) * 1.3.
Test Data:
Name |
Employee ID |
Level |
Hourly Rate |
Working hours |
Cindy |
D001 |
Engineer |
$30.57 |
40 |
Jeff |
D002 |
Manager |
$36.25 |
48 |
Tom |
D003 |
Director |
$40.05 |
42 |
Program screenshots:
Sample output:
Code to copy:
// Include the required header files.
#include<iostream>
// Use the standard namespace.
using namespace std;
// Create an enum to store the levels.
enum Level {ENGINEER, MANAGER, DIRECTOR};
// Create a structure to store the employee's data.
struct employee
{
// Create string of characters to store the
// name and id of the employee.
char Name[25];
char emp_id[10];
char gender;
// Declare an enum to store the level.
Level level;
// Declare the required floating-point variables.
float hourly_rate;
float working_hours;
float weekly_salary;
};
// Define the main() function.
int main()
{
// Declare the required variables.
char choice = 'Y';
float overtime;
string level;
employee emp;
// Start the loop to store the user input.
while(choice == 'Y')
{
// Prompt the user to enter the values.
cout << "Enter the name of the employee: ";
cin >> emp.Name;
cout << "Enter the ID of the employee: ";
cin >> emp.emp_id;
cout << "Enter the gender of the employee (M/F): ";
cin >> emp.gender;
cout << "Enter the level of the employee: ";
cin >> level;
// Check the user input and set the enum value
// for the employee.
if(level == "Engineer" || level == "engineer")
{
emp.level = ENGINEER;
}
else if(level == "Manager" || level == "manager")
{
emp.level = MANAGER;
}
else if(level == "Director" || level == "director")
{
emp.level = DIRECTOR;
}
else
{
cout << "Error: Invalid input!" << endl;
return 0;
}
// Prompt the user to enter the values.
cout << "Enter the hourly rate of the employee: ";
cin >> emp.hourly_rate;
cout << "Enter the working hours of the employee: ";
cin >> emp.working_hours;
// Display the details of the employee.
cout << endl << "The employee's data is as follows: " << endl;
cout << endl << "Name: " << emp.Name << endl;
cout << "Employee ID: " << emp.emp_id << endl;
cout << "Level: ";
// Use the switch condition to check and print
// the value of the enum level.
switch (emp.level)
{
case ENGINEER:
cout << "Engineer" << endl;
break;
case MANAGER:
cout << "Manager" << endl;
break;
case DIRECTOR:
cout << "Director" << endl;
break;
}
// Set the overtime to 0.
overtime = 0;
// Check if the number of working hours is
// greater than 40 or not.
if(emp.working_hours > 40)
{
// Calculate the salary for 40 hours.
emp.weekly_salary = emp.hourly_rate * 40;
// Use the switch condition to check and compute
// the overtime pays.
switch(emp.level)
{
case ENGINEER:
overtime = emp.hourly_rate * (emp.working_hours - 40) * 1.15;
break;
case MANAGER:
overtime = emp.hourly_rate * (emp.working_hours - 40) * 1.20;
break;
case DIRECTOR:
overtime = emp.hourly_rate * (emp.working_hours - 40) * 1.3;
break;
}
// Add the overtime pay in the weekly salary.
emp.weekly_salary += overtime;
}
// Compute the weekly salary for working hours
// less than or equal to 40.
else
{
emp.weekly_salary = emp.hourly_rate * emp.working_hours;
}
// Display the details.
cout << "Weekly salary: $" << emp.weekly_salary << endl;
cout << "Gender: " << emp.gender << endl << endl;
// Prompt the user to enter the choice.
cout << "Do you want to add another employee? (Y/N): ";
cin >> choice;
cout << endl;
}
// Return 0 and exit the program.
return 0;
}