Question: The purpose of this C++ programming project is to allow the student to perform parallel array and multidimensional array processing. The logic for string and

The purpose of this C++ programming project is to allow the student to perform parallel array and multidimensional array processing. The logic for string and Cstring has already been completed, so the assignment can be started before we actually cover string and Cstring in detail. This program has the following three menu options:

 A: List Payroll Information by Employee Name B: Search Payroll Information by Employee Name X: Exit the Payroll Information Module Options A and B have already been developed and tested. Your assignment is to copy-and-paste the source code below, and implement the function that accumulates the total hours and calculates the average for each employee.  Input File: Add a text file to project P10 named P10.txt. This file will serve as the input file. Enter or copy-and-paste the following data into the text file: 1018 Smit Bruce 9.50 50 10 25 30 1005 Bob Billy 5.00 30 35 40 50 1002 Long_Last_Name1 Long_First_Name 8.00 50 50 10 40 1004 Flores Jose 9.00 35 40 50 60 1010 Smitts Scott 7.00 10 30 50 30 1001 Smith Joe 6.00 35 40 50 60 1007 Lee JJ 8.00 35 40 50 60 1006 Sanchez Maria 7.00 10 50 50 60 1009 Eldridge Melissa 6.50 20 20 30 40 1003 Jones Larry 10.00 25 45 30 35 Make sure to press enter after the last record, so that EOF test will work correctly. General Processing: The records stored in the payroll file P10.txt are loaded into arrays. All of the menu options are implemented by manipulating the arrays. The last name is loaded into a char array, and the first name is loaded into a string array. Option A - List Payroll Information by Last Name: The arrays are sorted by Name. After the sort, the data loaded in the arrays is displayed on the screen. The output has column headings, is neatly spaced, and at the bottom the total number of records loaded from the file is displayed. Option B - Search Payroll Information by Employee Name: Prompts the user to enter a name to search for. A local char array is declared to store the search criteria. Records that match the name entered, as well as those that partially match the criteria, are displayed. This is call a wildcard search. The strnicmp function is used because it ignores case for the comparison. Headings are displayed above the detail information of the matching record. An error message is displayed if a match was not found. Before searching, the arrays are sorted ascending so that an early exit to the search can be implemented. An early exit is when the loop is terminated because it has been determined that the search criteria is not in the list. This is when the table element is greater than the search criteria. Requirements: Create a new project (P10). Copy-and-paste the source code provided below. Review the code related to cstring and strings in sort and search by Name. Design and develop the code to implement the sumAndComputeAvgHours( ) function. Turn in: source code (P10.cpp) input file (P10.txt) sample output of the list option (A) with totals and average displayed. sample output of the search option (B) include copies of partial name matches include a copy of the early exit message include a copy of the name not found message Note: Only the initial for the first name is displayed. Sample Output Option A - List Payroll Information by Last Name: (Total and Average need to be computed before the values will be displayed.) P10 Your Name Here Employee Last name F. Rate Wk1 Wk2 Wk3 Wk4 Total Average Employee Last name F. Rate Wk1 Wk2 Wk3 Wk4 Total Average 1005 Bob B. 5.00 30 35 40 50 155 38.75 1009 Eldridge M. 6.50 20 20 30 40 110 27.50 1004 Flores J. 9.00 35 40 50 60 185 46.25 1003 Jones L. 10.00 25 45 30 35 135 33.75 1007 Lee J. 8.00 35 40 50 60 185 46.25 1002 Long_Last_Name1 L. 8.00 50 50 10 40 150 37.50 1006 Sanchez M. 7.00 10 50 50 60 170 42.50 1018 Smit B. 9.50 50 10 25 30 115 28.75 1001 Smith J. 6.00 35 40 50 60 185 46.25 1010 Smitts S. 7.00 10 30 50 30 120 30.00 Records Listed: 10 Sample Output Option B Search Payroll Information by Last Name: Enter the Employee Name to search for: Market Early exit...Employee Name not on file. Enter the Employee Name to search for: L Employee Last name F. Rate Wk1 Wk2 Wk3 Wk4 Total Average 1007 Lee J. 8.00 35 40 50 60 185 46.25 1002 Long_Last_Name1 L. 8.00 50 50 10 40 150 37.50 Early exit... Enter the Employee Name to search for: Smit Employee Last name F. Rate Wk1 Wk2 Wk3 Wk4 Total Average 1018 Smit B. 9.50 50 10 25 30 115 28.75 1001 Smith J. 6.00 35 40 50 60 185 46.25 1010 Smitts S. 7.00 10 30 50 30 120 30.00 Procedure completed. Press Enter to continue: Source Code: //P10 Array Processing /* This program uses simple arrays, multidimensional arrays, cstrings, strings, and files. It allows a payroll clerk to choose an option from a menu. The choices are: A: List the Payroll Information by Employee Name B: Search Payroll Information by Employee Name X: Exit the Payroll Information Module The following items for each employee are saved in the file p10.txt: Employee ID (1000 - 9999) Last Name (15 characters) First Name (15 characters) Rate (5.00 - 10.00) Hours W1,W2,W3,W4 (0-60) */ #include  // file processing #include  // cin and cout #include  // toupper #include  // setw #include  // cstring functions strlen, strcmp, strcpy stored in string.h #include  // string class using namespace std; //Disable warning messages C4267 C4996. //To see the warnings, comment out the following line. #pragma warning( disable : 4267 4996) //Warning C4267: coversion from size_t to int, possible lost of data //size_t is a data type defined in and is an unsigned integer. //The function strlen returns a value of the type size_t, but in //searchByName we assign the returned value to an int. //We could also declare the variable as size_t instead of int. // size_t stringLength; //Warning C4996: strnicmp strcpy, stricmp was declared deprecated, means //the compiler encountered a function that was marked with deprecated. //The deprecated function may no longer be supported in a future release. //Global Constants //When using to declare arrays, must be defined with const modifier const int ARRAY_SIZE = 20, HOURS_SIZE = 4, NAME_SIZE = 16; //Declare arrays as global so we don't have to pass the arrays to each function. //Normally we wouldn't declare variables that change values a global. int employeeId[ARRAY_SIZE]; string firstName[ARRAY_SIZE]; char lastName[ARRAY_SIZE][NAME_SIZE]; double rate[ARRAY_SIZE]; int hours[ARRAY_SIZE][HOURS_SIZE]; int numberOfEmps; //count of how many employees were loaded into arrays int sumHours[ARRAY_SIZE] = {0}; //initialize arrays to zero by providing a double avgHours[ARRAY_SIZE] = {0}; //value for the first element in the array //Function Prototypes void loadArray( ); void sumAndComputeAvgHours( ); void listByName( ); void searchByName( ); void sortByName( ); void swapValues(int i, int minIndex); void listEmployees( ); void listEmployeesHeadings( ); void listEmployeesDetails(int i); void listEmployeesTotals( ); void displayContinuePrompt( ); //Program starts here int main() { //Declare and initialize local main variables char choice; //menu option //Load the arrays with data loadArray(); //Sum and compute the average hours sumAndComputeAvgHours(); //check to see what the user wants to do do // while (choice != 'X') { cout << "P10 Your Name Here "; cout << "Enter the letter of the desired menu option. " << "Press the Enter key after entering the letter. " << " A: List Payroll Information by Employee Name " << " B: Search Payroll Information by Name " << " X: Exit the Payroll Information Module " << "Choice: "; cin >> choice; choice =toupper(choice); switch (choice) { case 'A': listByName( ); break; case 'B': searchByName( ); break; case 'X': cout << " Now exiting Payroll Information...please wait. "; break; default: cout << "\a Invalid Option Entered - Please try again. "; } // end of switch } while (choice != 'X'); return 0; } // end of main //Function Definitions void loadArray( ) { //Open the file for input; ifstream fileIn; fileIn.open("p10.txt"); //If there are any errors, display an error message and return. if (fileIn.fail()) { cout << endl << endl << "Error: Input file NOT found. " << endl << endl; displayContinuePrompt(); numberOfEmps = 0; return; } //Intialize index to load the 1st record into the 1st row of the array int i = 0; //Read the first record. Reads into the arrays fileIn >> employeeId[i] >> lastName[i] >> firstName[i] >> rate[i] >> hours[i][0] >> hours[i][1] >> hours[i][2] >> hours[i][3]; //Process the remaining records if any while (! fileIn.eof()) { i++; if (i == ARRAY_SIZE) { cout << endl << endl << "Warning: Array Size Exceeded! Size = " << ARRAY_SIZE << "Processing will continue. Proceed with caution." << endl << endl; break; // get out of while loop } fileIn >> employeeId[i] >> lastName[i] >> firstName[i] >> rate[i] >> hours[i][0] >> hours[i][1] >> hours[i][2] >> hours[i][3]; } //Save the number of records loaded numberOfEmps = i; //close the file fileIn.close(); return; } //To be implemented by student  void sumAndComputeAvgHours( ) { //outer for loop walks through number of employees { //inner for loop walks through hours (HOURS_SIZE) { //accumlate each individual hour into sumHours array } //calculate emp's avgHours using their sumHours and HOURS_SIZE //sumHours and HOURS_SIZE are both integers, integer division will discard digits after the decimal point //Need to cast one of the operands as decimal so that the values are not discarded. //Review division and type casting in the textbook for more information. //Syntax: static_cast(HOURS_SIZE) } return; } //Opt A: List by Name void listByName( ) { sortByName( ); listEmployees( ); displayContinuePrompt( ); return; }  //Students are NOT required to do any coding involving cstrings, but //they are required to understand how to use them. Below are a few //examples of the functions used to manipulate cstrings.  //Opt B: Search by Name void searchByName( ) { //To facilitate the early exit logic, data must be sorted by name sortByName( ); //declare searching variables int stringLength; char searchName[NAME_SIZE]; bool recordFound = false; bool headingsDisplayed = false; //Since we are supporting a wildcard search, there could be more than //one lastName that matches the search criteria. However, we only //want to display the headings after the first match and not for the //subsequent matches. We use headingsDisplayed for this logic. cout << " Enter the Employee Name to search for: "; cin >> searchName; //We only compare upto the length of the search string stringLength = strlen(searchName); // '\0' not included in length //strncmp is case sensitive and strnicmp ignores case for (int i = 0; i < numberOfEmps; i++) //walk through the array { if (strnicmp(lastName[i],searchName, stringLength) == 0) { if (! headingsDisplayed) { listEmployeesHeadings(); //display the headings to cout headingsDisplayed = true; //only after 1st match } //display the record found recordFound = true; listEmployeesDetails(i); //don't get out of the for-loop, because there may be more matches } else if (strnicmp(lastName[i],searchName, stringLength) > 0) //early exit { cout << endl << "Early exit..."; break; //get out of for-loop } } if (false == recordFound) { cout << "Employee Name not on file. "; } displayContinuePrompt(); return; } void sortByName( ) { //First for-loop walks through the entire array //The current entry is saved as the min value //Second for-loop looks for values lower than the //current value. If one is found, then all values are swapped. int minIndex; char minName[NAME_SIZE]; for (int i = 0; i < (numberOfEmps - 1); i++) { minIndex = i; strcpy(minName,lastName[i]); for (int i2 = i + 1; i2 < numberOfEmps; i2++) { if (stricmp(lastName[i2],minName) < 0) //if str1 < str2 { //a neg number returned minIndex = i2; strcpy(minName,lastName[i2]); } } swapValues(i, minIndex); } return; } void swapValues(int i, int minIndex) { //temp holding variables int holdInt; char holdName[NAME_SIZE]; string holdStr; double holdDbl; holdInt = employeeId[i]; employeeId[i] = employeeId[minIndex]; employeeId[minIndex] = holdInt; //lastName is a cstring - use functions to manipulate strcpy(holdName,lastName[i]); strcpy(lastName[i],lastName[minIndex]); strcpy(lastName[minIndex],holdName); //firstName is a string object - use overloaded operators holdStr = firstName[i]; firstName[i] = firstName[minIndex]; firstName[minIndex] = holdStr; holdDbl = rate[i]; rate[i] = rate[minIndex]; rate[minIndex] = holdDbl; holdInt = hours[i][0]; hours[i][0] = hours[minIndex][0]; hours[minIndex][0] = holdInt; holdInt = hours[i][1]; hours[i][1] = hours[minIndex][1]; hours[minIndex][1] = holdInt; holdInt = hours[i][2]; hours[i][2] = hours[minIndex][2]; hours[minIndex][2] = holdInt; holdInt = hours[i][3]; hours[i][3] = hours[minIndex][3]; hours[minIndex][3] = holdInt; holdInt = sumHours[i]; sumHours[i] = sumHours[minIndex]; sumHours[minIndex] = holdInt; holdDbl = avgHours[i]; avgHours[i] = avgHours[minIndex]; avgHours[minIndex] = holdDbl; return; } void listEmployees( ) { listEmployeesHeadings(); for (int i = 0; i < numberOfEmps; i++) { listEmployeesDetails(i); } listEmployeesTotals( ); return; } void listEmployeesHeadings() { cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << " P10 Your Name Here " << "Employee Last name F. " << "Rate Wk1 Wk2 Wk3 Wk4 Total Average "; return; } void listEmployeesDetails(int i) { cout << setw(6) << employeeId[i] << " "; cout.setf(ios::left); cout << setw(17) << lastName[i] << firstName[i][0] << "."; cout.unsetf(ios::left); cout << setw(9) << rate[i]; for (int i2 = 0; i2 < HOURS_SIZE; i2++) // i2 = 0,1,2,3 { cout << setw(6) << hours[i][i2]; } cout << setw(7) << sumHours[i] << setw(9) << avgHours[i] << endl; return; } void listEmployeesTotals( ) { cout << " Records Listed: " << numberOfEmps << endl << endl; return; } void displayContinuePrompt() { char prompt; cout << " Procedure completed. Press Enter to continue: "; cin.ignore(); prompt = cin.get(); system("cls"); //clear screen - DOS return; } //End of program 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!