SlideShare a Scribd company logo
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:
Solution
/*
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
#define stricmp strcasecmp
#define strnicmp strncasecmp
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
for (int i = 0; i > searchName;
//We only compare upto the length of the search string
stringLength = strlen(searchName); // '0' not included in length
//strncmp is case sensitive and strncasecmp ignores case
for (int i = 0; i < numberOfEmps; i++) //walk through the array
{
if (strncasecmp(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 (strncasecmp(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 (strcasecmp(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
/*
output:
P10 Your Name Here
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: A
P10 Your Name Here
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
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: 9
Procedure completed. Press Enter to continue:
P10 Your Name Here
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: A
P10 Your Name Here
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
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: 9
Procedure completed. Press Enter to continue:
P10 Your Name Here
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: B
Enter the Employee Name to search for: S
P10 Your Name Here
Employee Last name F. Rate Wk1 Wk2 Wk3 Wk4 Total Average
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
Procedure completed. Press Enter to continue:
P10 Your Name Here
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: B
Enter the Employee Name to search for: La
Early exit...Employee Name not on file.
Procedure completed. Press Enter to continue:
P10 Your Name Here
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: X
Now exiting Payroll Information...please wait.
*/

More Related Content

DOCX
Classes(or Libraries)#include #include #include #include.docx
DOCX
computer project code ''payroll'' (based on datafile handling)
RTF
project3
PDF
I need help to modify my code according to the instructions- Modify th.pdf
PDF
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
PDF
Having issues with passing my values through different functions aft.pdf
PDF
To write a program that implements the following C++ concepts 1. Dat.pdf
PDF
Below is my code- I have an error that I still have difficulty figurin.pdf
Classes(or Libraries)#include #include #include #include.docx
computer project code ''payroll'' (based on datafile handling)
project3
I need help to modify my code according to the instructions- Modify th.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
Having issues with passing my values through different functions aft.pdf
To write a program that implements the following C++ concepts 1. Dat.pdf
Below is my code- I have an error that I still have difficulty figurin.pdf

Similar to The purpose of this C++ programming project is to allow the student .pdf (20)

PDF
#include iostream #include fstream #include string #incl.pdf
DOCX
In this assignment, you will continue working on your application..docx
DOC
CBSE Class XII Comp sc practical file
DOCX
Computer science project work
DOCX
12th CBSE Practical File
DOCX
Pratik Bakane C++
DOC
Practical Class 12th (c++programs+sql queries and output)
DOC
Practical Class 12th (c++programs+sql queries and output)
PDF
java programming languageThe attached A12.txt file which has 2 col.pdf
PDF
Using C++...Hints- Use binary file readwrite to store employee .pdf
PDF
#include iostream #include fstream #include vector #incl.pdf
DOCX
Be sure to read all of Chapters 8 and 9 before starting this assignm.docx
PDF
in C++ Design a class named Employee The class should keep .pdf
PDF
I keep on get a redefinition error and an undefined error.Customer.pdf
PPTX
Data structure array
PDF
This code has nine errors- but I don't know how to solve it- Please g.pdf
DOCX
PDF
Rajeev oops 2nd march
PPTX
Employee management system
PPTX
OOPS 22-23 (1).pptx
#include iostream #include fstream #include string #incl.pdf
In this assignment, you will continue working on your application..docx
CBSE Class XII Comp sc practical file
Computer science project work
12th CBSE Practical File
Pratik Bakane C++
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
java programming languageThe attached A12.txt file which has 2 col.pdf
Using C++...Hints- Use binary file readwrite to store employee .pdf
#include iostream #include fstream #include vector #incl.pdf
Be sure to read all of Chapters 8 and 9 before starting this assignm.docx
in C++ Design a class named Employee The class should keep .pdf
I keep on get a redefinition error and an undefined error.Customer.pdf
Data structure array
This code has nine errors- but I don't know how to solve it- Please g.pdf
Rajeev oops 2nd march
Employee management system
OOPS 22-23 (1).pptx
Ad

More from Rahul04August (20)

PDF
Why soot formation is only observed in diffusion flames I need deta.pdf
PDF
Which one is the correct answer Which is not a KEY characteristic o.pdf
PDF
Which of the following statements about prokaryotes is falsea) Prok.pdf
PDF
What type of computer would integrate the system unit (chassis and co.pdf
PDF
What is the physical meaning of each term in the below equation DM_.pdf
PDF
What is the difference between a nucleotide sequence and a protein s.pdf
PDF
types of becterias and diseases that come from of wrong storing food.pdf
PDF
Two white mice mate. The male has both a white and black fur-colo.pdf
PDF
Two chains are available to hold up an object. A student arranges t.pdf
PDF
This is a modeling problem in Interger programming.We have a probl.pdf
PDF
There is a surge of mergers and acquisitions among television networ.pdf
PDF
the input to a frequency translation system has a bandwidth of 19 to.pdf
PDF
The First genetic maps used genes as markers becauseplease answer.pdf
PDF
the consistent joining of two specific bases in nucleotides within DN.pdf
PDF
Technology is advancing at an alarming rate. We now have more method.pdf
PDF
Suppose a mutant fruit fly with blue eyes was recently discovered. I.pdf
PDF
Students should research and identify an article on the subject of s.pdf
PDF
Questions1. Research the Master Boot Record. Write a long paragra.pdf
PDF
Problem Find the z-transform, in closed form, of the number sequence .pdf
PDF
Please give me as detailed as possible answers! Im really struggli.pdf
Why soot formation is only observed in diffusion flames I need deta.pdf
Which one is the correct answer Which is not a KEY characteristic o.pdf
Which of the following statements about prokaryotes is falsea) Prok.pdf
What type of computer would integrate the system unit (chassis and co.pdf
What is the physical meaning of each term in the below equation DM_.pdf
What is the difference between a nucleotide sequence and a protein s.pdf
types of becterias and diseases that come from of wrong storing food.pdf
Two white mice mate. The male has both a white and black fur-colo.pdf
Two chains are available to hold up an object. A student arranges t.pdf
This is a modeling problem in Interger programming.We have a probl.pdf
There is a surge of mergers and acquisitions among television networ.pdf
the input to a frequency translation system has a bandwidth of 19 to.pdf
The First genetic maps used genes as markers becauseplease answer.pdf
the consistent joining of two specific bases in nucleotides within DN.pdf
Technology is advancing at an alarming rate. We now have more method.pdf
Suppose a mutant fruit fly with blue eyes was recently discovered. I.pdf
Students should research and identify an article on the subject of s.pdf
Questions1. Research the Master Boot Record. Write a long paragra.pdf
Problem Find the z-transform, in closed form, of the number sequence .pdf
Please give me as detailed as possible answers! Im really struggli.pdf
Ad

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Insiders guide to clinical Medicine.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Basic Mud Logging Guide for educational purpose
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Institutional Correction lecture only . . .
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Insiders guide to clinical Medicine.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Anesthesia in Laparoscopic Surgery in India
PPH.pptx obstetrics and gynecology in nursing
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Basic Mud Logging Guide for educational purpose
O5-L3 Freight Transport Ops (International) V1.pdf
Institutional Correction lecture only . . .
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
GDM (1) (1).pptx small presentation for students
STATICS OF THE RIGID BODIES Hibbelers.pdf
Sports Quiz easy sports quiz sports quiz
Renaissance Architecture: A Journey from Faith to Humanism

The purpose of this C++ programming project is to allow the student .pdf

  • 1. 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: Solution /* 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 #define stricmp strcasecmp #define strnicmp strncasecmp 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
  • 2. //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
  • 3. 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;
  • 4. } // 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]; }
  • 5. //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 for (int i = 0; i > searchName; //We only compare upto the length of the search string stringLength = strlen(searchName); // '0' not included in length //strncmp is case sensitive and strncasecmp ignores case for (int i = 0; i < numberOfEmps; i++) //walk through the array { if (strncasecmp(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 (strncasecmp(lastName[i],searchName, stringLength) > 0) //early exit { cout << endl << "Early exit..."; break; //get out of for-loop } } if (false == recordFound) {
  • 6. 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 (strcasecmp(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];
  • 7. 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; }
  • 8. 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]; }
  • 9. 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 /* output: P10 Your Name Here 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: A P10 Your Name Here
  • 10. 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 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: 9 Procedure completed. Press Enter to continue: P10 Your Name Here 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: A P10 Your Name Here 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 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: 9 Procedure completed. Press Enter to continue: P10 Your Name Here
  • 11. 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: B Enter the Employee Name to search for: S P10 Your Name Here Employee Last name F. Rate Wk1 Wk2 Wk3 Wk4 Total Average 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 Procedure completed. Press Enter to continue: P10 Your Name Here 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: B Enter the Employee Name to search for: La Early exit...Employee Name not on file. Procedure completed. Press Enter to continue: P10 Your Name Here Enter the letter of the desired menu option. Press the Enter key after entering the letter.
  • 12. A: List Payroll Information by Employee Name B: Search Payroll Information by Name X: Exit the Payroll Information Module Choice: X Now exiting Payroll Information...please wait. */