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
sumAndComputeAv.
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];
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.
*/