Create a menu based program for Employee records The menu wi

Create a menu based program for Employee records. The menu will prompt to either add a new employee, print one employee, print all employees, or exit.

OUTPUT SHOULD LOOK LIKE THIS:

For the Employee structure definition, the employee will have a static sized cstring for the employee\'s first and last name, an integer for the employee\'s hours worked, and a float for the employee\'s hourly payrate.

This program will use an Employee pointer to store the array of employees, and an integer to store the current count of employees.
For option 1, each time an employee is added, you will need to allocate a new temporary array with count + 1 employees, copy the current array into the temporary (memberwise assignment), deallocate the current, then set the current to point to the temporary. You will write a function called \'allocate\' to do this, description below.
For option 2, you will prompt the user for which indexed employee to print. If the user ask for the first employee by entering 1, you will print the contents of the employee stored in the array at index 0. You will validate that the entered index is within range of the employee array (1<=index<=count). If the index is out of range, prompt the user again for a valid index.
For option 3, you will pass the employee array and the current count to a function that will traverse the array, print the members of each object, and calculate the $paycheck.00 (rate*hours) in nice formatted output.
For option 0, you will then deallocate the employee array if necessary, set the employee pointer to NULL, and exit the program.

Functions

Employee* allocate(Employee* &, int&); - This function will pass the current employee array and the current count by reference. Allocate a new temporary array with count+1 capacity. If the current employee array does not point to NULL, copy the current array to the temporary array, then deallocate the current array, and return the temporary array back to main (the function call being assinged into the employee pointer).

void inputEmployee(Employee&); - This function will prompt the user to enter values and store the inputed data into the object\'s members.

void printEmployee(const Employee&); - This function will print the members of the passed object in nice formatted output.

void printAllEmployees(const Employee*, int); - This function will pass the employee array and the current count of employees. Print all the employees\' members and calculate the employee\'s paycheck (hours*payrate) in nice formatted output.

This code is written in C++, not Java.

Solution

#include<iostream>
#include<iomanip>
#include<string.h>
#define MAX 15
using namespace std;

struct emp
{
   //declare firstname
   char fname[MAX];
   //declare lastname
   char lname[MAX];
   int hours_worked;
   float hourly_payrate;
};

typedef struct emp Employee;
//function declarations
Employee* allocate(Employee* &, int &);
void inputEmployee(Employee &);
void printEmployee(const Employee &);
void printAllEmployees(const Employee*, int);
int main()
{
   int choice,count = 0;
   Employee *record = NULL;
   do
   {
       cout << \"#############MENU################\" << endl;
       cout << \"*1.Add new employee *\" << endl;
       cout << \"*2.Print one employee*\" << endl;
       cout << \"*3.Print all employee *\" << endl;
       cout << \"*0.Exit *\" << endl;
       cin >> choice;
       switch (choice)
       {
           case 1:
              
               record = allocate(record, ++count);
               inputEmployee(record[count-1]);
              
               break;
           case 2:
               int index;
               cout << \"Enter index of the employee to print: \";
               cin >> index;
               printEmployee(record[index-1]);
               break;
           case 3:
               printAllEmployees(record, count);
              
               break;
           case 0:
               cout << \"Exiting ...\";
               break;

       }
   } while (choice != 0);
}

Employee* allocate(Employee* &rec, int &count)
{
   Employee *tmp = NULL;

   //allocate array with count+1
   tmp = new Employee[count + 1];
   if (rec == NULL)
   {
       return tmp;
   }
   else
   {
       for (int i = 0; i < count-1; i++)
       {
           strcpy(tmp[i].fname,rec[i].fname);
           strcpy(tmp[i].lname, rec[i].lname);
           tmp[i].hours_worked = rec[i].hours_worked;
           tmp[i].hourly_payrate = rec[i].hourly_payrate;
       }
       return tmp;
   }

  
}
void inputEmployee(Employee &record)
{
   cout << \"Enter first name: \";
   cin >> record.fname;
   cout << \"\ Enter last name: \";
   cin >> record.lname;
   cout << \"\ Enter no of hours worked: \";
   cin >> record.hours_worked;
   cout << \"\ Enter hourly rate: \";
   cin >> record.hourly_payrate;
}
void printEmployee(const Employee &rec)
{
   cout << \" First_Name\" << setw(15) << \" Last_Name\" << setw(3) << \" Hours_worked\" << \" Pay_rate\" <<setw(8)<<\" $Paycheck\" <<endl;
   cout << setw(15) << rec.fname << setw(15) << rec.lname << setw(15) << rec.hours_worked << setw(8) << rec.hourly_payrate << setw(10) << rec.hours_worked *rec.hourly_payrate << endl;
}

void printAllEmployees(const Employee *rec, int count)
{
   cout << \" First_Name\" << setw(15) << \" Last_Name\" << setw(3) << \" Hours_worked\" << \" Pay_rate\" << setw(8) << \" $Paycheck\" << endl;
   for (int i = 0; i < count; i++)
   {
       cout <<setw(15) << rec[i].fname << setw(15) << rec[i].lname << setw(15) << rec[i].hours_worked << setw(8) << rec[i].hourly_payrate << setw(10) << rec[i].hours_worked *rec[i].hourly_payrate << endl;
       //cout << i+1<<\".\"<<setw(15) << rec[i].fname << setw(15) << rec[i].lname << setw(3) << rec[i].hours_worked << \"\\t\" << setw(6) << \" \"<<rec[i].hourly_payrate << setw(8) << rec[i].hours_worked *rec[i].hourly_payrate <<endl;
   }
}

-------------------------------------------------------------------------------------------------

//output

*1.Add new employee *
*2.Print one employee*
*3.Print all employee *
*0.Exit *
2
Enter index of the employee to print: 2
First_Name Last_Name Hours_worked Pay_rate $Paycheck
Smith fg 50 120 6000
#############MENU################
*1.Add new employee *
*2.Print one employee*
*3.Print all employee *
*0.Exit *
3
First_Name Last_Name Hours_worked Pay_rate $Paycheck
Jacob Lk 40 120 4800
Smith fg 50 120 6000
Samanth D 35 200 7000
Donald j 46 140 6440
#############MENU################
*1.Add new employee *
*2.Print one employee*
*3.Print all employee *
*0.Exit *

0
Exiting ...

Create a menu based program for Employee records. The menu will prompt to either add a new employee, print one employee, print all employees, or exit. OUTPUT SH
Create a menu based program for Employee records. The menu will prompt to either add a new employee, print one employee, print all employees, or exit. OUTPUT SH
Create a menu based program for Employee records. The menu will prompt to either add a new employee, print one employee, print all employees, or exit. OUTPUT SH
Create a menu based program for Employee records. The menu will prompt to either add a new employee, print one employee, print all employees, or exit. OUTPUT SH

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site