PLEASE HELP CODE PROBLEM

I'm having issues with the employee delete and the employee add parts of my code. When I try to delete an employee it gets deleted but then the spot it was in is filled with the bottom information (so it shoes up twice). Then with the add portion of my code I go through all the add information and then it doesn't show up in my list.

Help would be appreciated

Here is the description of my code:
Write a menu-driven C++ program to manage employee data. Your program should begin by reading in a file of employee information (filename employdata.txt). Each data line contains the following information:


{hire date} {employee ID} {salary}

Note that all data values can be stored as integers. The dates included are integers but in a strict format: yyyymmdd. Read these data values into three parallel arrays for further processing.

Write a menu-driven program that offers the user the following options.
•List by hire date
•List by employee number
•Write total of salaries
•Add employee
•Delete employee

The "list" functions imply sorting the arrays either by hire date or employee number and then writing the entire data set. Be sure that it is written in an organized and readable format. The function to add an employee requires prompting for a new element for all three arrays. Be sure the elements are added and kept in "parallel" format. For the function to delete an element, prompt the user for an employee number and then delete that element from all three arrays.

Be sure to consider modularity in this program. Obvious functions to implement include the displaying the menu, adding and deleting an employee, as well as the sorting/display actions.


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
  // 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 += 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 
	}
}
I guess it's not an error, but what's line 41 doing there? You declare a variable and don't use it.

Why do you have two functions doing the same thing? (DisplayList and showarray)

Your function sortarray should be renamed to something like sortArrayByID and you need a second function sortArrayByHireDate. Otherwise, what would happen if the user chose 1, then 2 in your menu? (This is even assuming that the data comes pre-sorted by hire date.)

In UnOrdInsert, totalRecs++; should come at the end of the function.

The descriptions for UnOrdInsert and UnOrdDelete do not match what you're actually doing in the functions.

When I try to delete an employee it gets deleted but then the spot it was in is filled with the bottom information (so it shoes up twice).

Are you sure it shows up twice? Because totalRecs--; // Decrement size of list in UnOrdDelete should make it so that the second one is not considered a part of the list, so it shouldn't show.
Okay I changed some names like you said. and added the "totalRecs++". And yes when I go to delete it doesn't work correctly
Bellow is what shows up. When I tell it to delete ID 123 it deletes it but it puts the bottom information in the spot that the deleted information was (as you can see in the first line where 123 what). I'm really not sure why its doing this. Also, when I try to add information to the list it still doesn't show up when I display the list


20081203 983 88865
20081015 238 53560
20080801 323 95474
20081129 467 78432
20081123 545 60965
20080505 563 66456
20080620 576 77456
20080820 753 74335
20090105 776 65678
20080912 854 24353
20081203 983 88865


Here are the changes
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
211
// 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 sortarraybyID(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 Insert(int hiredate[], int employee[], int salary[], int totalRecs);
void Delete(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: sortarraybyID(hiredate, employee, salary, totalRecs);
				showarray(hiredate, employee, salary, totalRecs);
				break;
			case 3: Sum = sum(salary, totalRecs);
				cout << "Sum = " << Sum << endl;
				break;
			case 4: Insert(hiredate, employee, salary, totalRecs);
				DisplayList(hiredate, employee, salary, totalRecs);
				break;
			case 5: Delete(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 sortarraybyID(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 += 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 Insert(int hiredate[], int employee[], int salary[], int totalRecs)
{
	int newint, newint2, newint3;


	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;

	totalRecs++; // Increment size of list
}


// 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 Delete(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 
	}
}


Don't know how I missed it before, but you are passing totalRecs to your insert and delete functions as a value, when it should be by reference. So use int& totalRecs, not int totalRecs.
oh my gosh. Thank you.
Topic archived. No new replies allowed.