Homework 9

2010_F18/wk9/hw9.cpp

The purpose of this homework is to understand functions via passing arrays as function arguments, cstrings, and parallel arrays.

For this homework, you will be writing a menu based employee paycheck calculator and employee records program. The menu options will include adding an employee record to the database, print all employee record(s), print the paycheck for one employee, and print the paychecks for all employees. Function prototypes and most of function main will be given to you. Your task is to complete function main's cases and write the following function definitions:

void addEmployee(char[][NAMELEN], double[], double[], int&);   - this function will pass in a cstring, two double arrays for payrate and hours, and an integer passed by reference for the current count of employees. Validate that count is less than MAX, then prompt the user for the employee's name, payrate, and hours and enter to the corresponding arrays at subscript count. Update the current count of employees and return. If the current value of count is not less than MAX, alert the user that the database is full.

void displayAllEmployees(const char[][NAMELEN], const double[], const double[], int);   - this function will pass in an array of cstrings, two double arrays, and an integer for the current count of employees. This function will output all the employees' name, payrate, and hours.

void printOneEmployeePaycheck(const char[], double, double);   - this function will pass in a cstring, and two doubles. Calculate the employee's paycheck value and output the employee's name, payrate, hours worked, and the calculated paycheck value (setprecision to 2).

void printAllEmployeePaychecks(const char[][NAMELEN], const double[], const double[], int);   - this function will pass in an array of cstrings, two double arrays, and an integer for the current count of employees. This function will output all the employees' name, payrate, hours worked, and calculated paycheck value (setprecision to 2).

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

#define MAX  20
#define NAMELEN 65

void printMenu();
void addEmployee(char[][NAMELEN], double[], double[], int&);
void displayAllEmployees(const char[][NAMELEN], const double[], const double[], int);
void printOneEmployeePaycheck(const char[], double, double);
void printAllEmployeePaychecks(const char[][NAMELEN], const double[], const double[], int);

int main() {
    int index=0, count=0, selection;
    char emp[MAX][NAMELEN];        // an array of cstrings
    double rate[MAX], hours[MAX];
    do{
        printMenu();
        cin >> selection;
        if( count==0 && selection !=1 && selection != 0) {
            cout << "You must add atleast one employee to the database!\n";
            continue;
        }
        switch(selection) {
            case 1:
              // call function addEmployee
              break;
            case 2:
              // call function displayAllEmployees
              break;
            case 3:
              do{
                  cout << "Please enter the index of the employee: ";
                  cin >> index;
              }while(index<0 || index>=count);
              // call function printOneEmployeePaycheck
              break;
            case 4:
              // call function printAllEmployeePaychecks
              break;
            case 0:
              break;
            default:
              cout << "Invalid choice\n";
              break;
        }
    }while(selection!=0);
    cout << "Exiting Program\n";
    return(0);
}

void printMenu() {
  cout << "\n\nWelcome to the Employee Paycheck Calculator\n"
  <<"===========================================\n"
  <<"1.  Add an employee record to the database\n"
  <<"2.  Print all employee records\n"
  <<"3.  Print the paycheck for one employee\n"
  <<"4.  Print paychecks for all employees\n"
  <<"0.  Exit\n"
  <<"===========================================\n"
  <<"Enter selection:  ";
}