In: Computer Science
Purpose
Review and reinforcement of pointers, dynamic memory allocation, pointer arithmetic, passing pointers to a function, returning a pointer by a function, dangling pointer, and memory deallocation, pointer initialization, and struct data type.
Project description
In this project, you will create a database of employees of an organization while meeting the requirements described below. Your program MUST NOT interact with the user to receive inputs, so that the instructor and/or the teaching assistant can save a big time in testing programs.
Employee is a struct data type that has the following attributes, or data members, or data fields
struct Employee {
string firstName;
string lastName;
int SSN;
string department;
Role role;
double salary;
};
The data type Role is defined as below.
enum Role {programmer, manager, director};
Define Employee.h and include the two data types above in the file. Role must be defined before Employee because Employee uses Role.
Screenshot
Program
Employee.h
#include<string>
using namespace std;
enum Role { programmer, manager, director };
struct Employee {
string firstName;
string lastName;
int SSN;
string department;
Role role;
double salary;
};
Employeemain.cpp
#include <iostream>
#include<iomanip>
#include "Employee.h"
using namespace std;
//Function prototypes
void setSalaries(Employee*,int);
void setRoles(Employee*, int);
void setOther(Employee*, int);
void displayEmployeeIndormation(Employee*, int);
void displayAvgSalry(Employee*, int);
void displayProgrammerInformation(Employee*, int);
int main()
{
//Create an array of employee
Employee *employees;
//Allocate size dynamically
employees = new Employee[5];
//Call setter functions
setSalaries(employees, 5);
setRoles(employees, 5);
setOther(employees, 5);
//Call display functions
displayEmployeeIndormation(employees, 5);
displayAvgSalry(employees, 5);
displayProgrammerInformation(employees, 5);
//Call memory deallocation
delete[] employees;
}
//Function to set salary of each employee between 45000 -
65000
void setSalaries(Employee* employees, int cnt) {
for (int i = 0; i < cnt; i++) {
employees[i].salary = 45000 +
(std::rand() % (65000 - 45000 + 1));
}
}
//Function to set roles of each employee
void setRoles(Employee* employees, int cnt) {
for (int i = 0; i < cnt; i++) {
employees[i].role =Role(rand() %
3);
}
}
//Function to set Other values of employees
void setOther(Employee* employees, int cnt) {
char consonents[] = {
'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'
};
char vowels[] = { 'a','e','i','o','u','y' };
string departments[] = {
"Accounts","Sales","Manufacturing","Technical" };
for (int i = 0; i < cnt; i++) {
string name1 = "", name2 =
"";
int random = rand() % 2;
int count = 0;
for (int i = 0; i < 5; i++)
{
if (random
< 2 && count < 2) {
name1 = name1 + consonents[rand() % 19];
name2 = name2 + consonents[rand() % 19];
count++;
}
else {
name1 = name1 + vowels[rand() % 5];
name2 = name2 + vowels[rand() % 5];
count = 0;
}
random =
rand() % 2;
}
employees[i].firstName =
name1;
employees[i].lastName =
name2;
employees[i].SSN = i + 10;
employees[i].department =
departments[rand() % 4];
}
}
//Function to display all employees information
void displayEmployeeIndormation(Employee* employees, int cnt)
{
cout << "\nEMPLOYEE DETAILS:-" <<
endl;
for (int i = 0; i < 74; i++) {
cout << "-";
}
cout << "\n| FirstName |
LastName | SSN |
Department | Role |
Salary |" << endl;
for (int i = 0; i < 74; i++) {
cout << "-";
}
cout << endl;
for (int i = 0; i < cnt; i++) {
cout << "| " << setw(8)
<< employees[i].firstName << setw(6) << "|"
<< setw(8)
<< employees[i].lastName << setw(6) << "|"
<< setw(4)
<< employees[i].SSN << setw(5) << "|"
<< setw(8)
<< employees[i].department << setw(8) <<
"|";
if (employees[i].role == 0) {
cout <<
setw(10) << "Programmer" << "|";
}
else if (employees[i].role == 1)
{
cout <<
setw(8) << "Manager" << setw(3) << "|";
}
else if (employees[i].role == 2)
{
cout <<
setw(8) << "Director" << setw(3) << "|";
}
cout << setw(6) <<
employees[i].salary << setw(3) << "|" <<
endl;;
}
for (int i = 0; i < 74; i++) {
cout << "-";
}
cout << endl;
}
//Function to display average salary
void displayAvgSalry(Employee* employees, int cnt) {
double avgSal = 0;
for (int i = 0; i < cnt; i++) {
avgSal +=
employees[i].salary;
}
cout << "\nAverage Salary = " << (avgSal /
cnt) << endl;
}
//Function to display programmer information
void displayProgrammerInformation(Employee* employees, int cnt)
{
cout << "\nPROGRAMMER DETAILS:-" <<
endl;
for (int i = 0; i < 74; i++) {
cout << "-";
}
cout << "\n| FirstName |
LastName | SSN |
Department | Role |
Salary |" << endl;
for (int i = 0; i < 74; i++) {
cout << "-";
}
cout << endl;
for (int i = 0; i < cnt; i++) {
if (employees[i].role == 0) {
cout << "|
" << setw(8) << employees[i].firstName << setw(6)
<< "|"
<< setw(8) << employees[i].lastName
<< setw(6) << "|"
<< setw(4) << employees[i].SSN
<< setw(5) << "|"
<< setw(8) <<
employees[i].department << setw(8) << "|"
<< setw(10) << "Programmer" <<
"|"
<< setw(6) << employees[i].salary
<< setw(3) << "|" << endl;
}
}
for (int i = 0; i < 74; i++) {
cout << "-";
}
cout << endl;
}
---------------------------------------------------------------
Output
EMPLOYEE DETAILS:-
--------------------------------------------------------------------------
| FirstName | LastName |
SSN | Department |
Role | Salary |
--------------------------------------------------------------------------
| jhitc |
jviqk | 10 |
Sales | Manager | 45041 |
| tmufr |
dlipg | 11 |
Sales |Programmer| 63467
|
| mbupr |
wmitw | 12
|Accounts |Programmer| 51334
|
| wnuwq |
xtojw | 13 |
Sales | Manager | 51499 |
| spagj |
dgeqd | 14 |
Sales |Director | 64169 |
--------------------------------------------------------------------------
Average Salary = 55102
PROGRAMMER DETAILS:-
--------------------------------------------------------------------------
| FirstName | LastName |
SSN | Department |
Role | Salary |
--------------------------------------------------------------------------
| tmufr |
dlipg | 11 |
Sales |Programmer| 63467
|
| mbupr |
wmitw | 12
|Accounts |Programmer| 51334
|
--------------------------------------------------------------------------