1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
// This program manages employee data
// It reads a file of employee information and offeres
// several difference sorting options
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void GetList(ifstream& fileIn, int hiredate[], int employee[], int salary[], int& totalRecs);
void menuaction(int hiredate[], int employee[], int salary[], int totalRecs);
void DisplayList(int hiredate[], int employee[], int salary[], int totalRecs);
void sortarray(int hiredate[], int employee[], int salary[], int totalRecs);
void showarray(int hiredate[], int employee[], int salary[], int totalRec);
int sum(int salary[], int totalRecs);
void UnOrdInsert(int hiredate[], int employee[], int salary[], int totalRecs);
void UnOrdDelete(int hiredate[], int employee[], int salary[], int totalRecs);
const int MAX_LIST_SIZE = 25;
int main()
{
// Array of list elements
int hiredate[MAX_LIST_SIZE];
int employee[MAX_LIST_SIZE];
int salary[MAX_LIST_SIZE];
int totalRecs = 0;
// Set up file
ifstream fileIn;
fileIn.open("employdata.txt"); //Open The File
if (fileIn.fail()) // Test for file existence
{
cout << "Problem opening file";
exit(-1);
}
ifstream theData;
GetList(fileIn, hiredate, employee, salary, totalRecs);
menuaction(hiredate, employee, salary, totalRecs);
}
// This function reads integers from a file and stores the values in
// an array. It returns the loaded array and the number of elements
// in the array
void GetList(ifstream& InFile, int hiredate[], int employee[], int salary[], int& totalRecs)
{
int i = 0;
// Priming read
InFile >> hiredate[i] >> employee[i] >> salary[i];
while (!InFile.eof())
{
i++; // Add one to pointer
// continuation reads
InFile >> hiredate[i] >> employee[i] >> salary[i];
}
totalRecs = i;
}
// This Fuction is the Menu Action
void menuaction(int hiredate[], int employee[], int salary[], int totalRecs)
{
int choice, Sum;
do
{
cout << "\n\t\tEmployee Data Menu\n\n";
cout << "1. List by hiredate\n";
cout << "2. List by employee number\n";
cout << "3. Write total of salaries\n";
cout << "4. Add employee\n";
cout << "5. Delete employee\n";
cout << "6. TERMINATE SESSION\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice >= 1 && choice <= 5)
{
switch (choice)
{
case 1: DisplayList(hiredate, employee, salary, totalRecs);
break;
case 2: sortarray(hiredate, employee, salary, totalRecs);
showarray(hiredate, employee, salary, totalRecs);
break;
case 3: Sum = sum(salary, totalRecs);
cout << "Sum = " << sum << endl;
break;
case 4: UnOrdInsert(hiredate, employee, salary, totalRecs);
DisplayList(hiredate, employee, salary, totalRecs);
break;
case 5: UnOrdDelete(hiredate, employee, salary, totalRecs);
DisplayList(hiredate, employee, salary, totalRecs);
break;
}
}
else if (choice != 6)
{
cout << "The valid choices are 1 through 6.\n";
cout << "Please try again.\n";
}
} while (choice != 6);
}
//This function writes a list to console output
void DisplayList(int hiredate[], int employee[], int salary[], int totalRecs)
{
for (int i = 0; i < totalRecs; i++)
cout << hiredate[i] << " " << employee[i] << " " << salary[i] << " " << endl;
cout << endl;
}
// This functions Lists the employees by number using sorting
void sortarray(int hiredate[], int employee[], int salary[], int totalRecs)
{
int temp, temp2, temp3, end;
for (end = totalRecs - 1; end >= 0; end--)
{
for (int i = 0; i < end; i++)
{
if (employee[i] > employee[i + 1])
{
temp = employee[i];
temp2 = hiredate[i];
temp3 = salary[i];
employee[i] = employee[i + 1];
hiredate[i] = hiredate[i + 1];
salary[i] = salary[i + 1];
employee[i + 1] = temp;
hiredate[i + 1] = temp2;
salary[i + 1] = temp3;
}
}
}
}
void showarray(int hiredate[], int employee[], int salary[], int totalRecs)
{
for (int i = 0; i < totalRecs; i++)
cout << hiredate[i] << " " << employee[i] << " " << salary[i] << endl;
}
// This functions writes the total of the salaries
int sum(int salary[], int totalRecs)
{
int sum = 0;
for (int i = 0; i < totalRecs; i++)
sum = sum + salary[i];
return sum;
}
// This function receives an integer, an array containing
// an unordered list, and the size of the list. It inserts
// a new integer item at the end of the list
void UnOrdInsert(int hiredate[], int employee[], int salary[], int totalRecs)
{
int newint, newint2, newint3;
totalRecs++; // Increment size of list
cout << "Insert New Employee HIREDATE:" << endl;
cin >> newint;
hiredate[totalRecs] = newint;
cout << "Insert New Employee ID NUMBER:" << endl;
cin >> newint2;
employee[totalRecs] = newint2;
cout << "Insert New Employee's Salary:" << endl;
cin >> newint3;
salary[totalRecs] = newint3;
}
// This function receives an integer, an array containing
// an unordered list, and the size of the list. It locates
// and deletes the integer fromt he list
void UnOrdDelete(int hiredate[], int employee[], int salary[], int totalRecs)
{
int empnum = 0, ptr = 0;
cout << " Which employee do you wish to delete" << endl;
cin >> empnum;
// Scan list for deletion target
while (empnum != employee[ptr] && ptr < totalRecs)
ptr++;
if (ptr < totalRecs) // If target found, then
{
employee[ptr] = employee[totalRecs - 1]; // Last list item to overwrite target
hiredate[ptr] = hiredate[totalRecs - 1];
salary[ptr] = salary[totalRecs - 1];
totalRecs--; // Decrement size of list
}
}
|