PLEASE HELP PROGRAM ERROR

Hello. I'm having trouble getting this program to run. It keeps telling my I have an unutilized local variable but I cant figure it out.

Here is the description of the program

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
210
211
212
213
 // 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);
int sum(int salary[]);
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;

	// 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, totals;

	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\n";
		cout << "5. Delete employee\n\n";
		cout << "6. TERMINATE SESSION\n\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);
				DisplayList(hiredate, employee, salary, totalRecs);
				break;
			case 3: totals = sum(salary);
				DisplayList(hiredate, employee, salary, totalRecs);
				break;
			case 4: UnOrdInsert(hiredate, employee, salary, totalRecs);
				DisplayList(hiredate, employee, salary, totalRecs);
				break;
			case 5: UnOrdDelete(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 < MAX_LIST_SIZE; i++)

		cout << hiredate[i] << employee[i] << salary[i] << " ";
	cout << endl;
}
// This functions Lists the employees by number using sorting 
void sortarray(int hiredate[], int employee[], int salary[], int totalRecs)
{
	int temp, end;
	int i = 0;

	for (end = totalRecs - 1; end >= 0; end--)
	
		for (int i = 0; i < end; i++)
		{
			if (hiredate[i] > hiredate[i + 1])
			{
				temp = hiredate[i];
				i = hiredate[i + 1];
				hiredate[i + 1] = temp;
			}
		}
		for (int i = 0; i < end; i++)
		{
			if (employee[i] > employee[i + 1])
			{
				temp = employee[i];
				employee[i] = employee[i + 1];
				employee[i + 1] = temp;
			}
		}
		for (int i = 0; i < end; i++)
		
			if (employee[i] > employee[i + 1])
				temp = employee[i];
				employee[i] = employee[i + 1];
				employee[i + 1] = temp;
			
}

// This functions writes the total of the salaries
int sum(int salary[])
{
		int total = 0;

		for (int i = 0; i <= 15; i++)
				{
					total += salary[i];
			
				}

		return total;

}

// 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)
{
	totalRecs++; // Increment size of list

	cout << "Insert New Employee HIREDATE:" << endl;
	cin >> hiredate[totalRecs - 1];
	cout << "Insert New Employee ID NUMBER:" << endl;
	cin >> employee[totalRecs - 1];
	cout << "Insert New Employee's Salary:" << endl;
	cin >> salary[totalRecs - 1];

}

// 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 
		totalRecs--; // Decrement size of list 
	}
}
Im not gonna read through all of it. Copy paste the errors in your post so I can read them.

Edit: Try initializing int totalRecs;.

int totalRecs = 0;
Last edited on
Can you change ...
 
void GetList(ifstream& fileIn, int hiredate[], int employee[], int salary[], int totalRecs);


with

 
void GetList(ifstream& fileIn, int hiredate[], int employee[], int salary[], int & totalRecs);


??
I tried changing the void statement. I get this error "error C4700: uninitialized local variable 'totalRecs' used" line 42
Dude... read my post..............


try initializing int totalRecs;.

int totalRecs = 0;

That worked thanks, but for some reason the only sort that works is option 1
I cant get my sort to work. It wont sort the function by employee number
Thats because you're not using the bubble sort function quote right. You need nested for-loops. Watch this video - https://www.youtube.com/watch?v=TVL5_1NWviA&list=PLHJE4y54mpC6R41pN7sEJGO4AbuE-NnGQ&index=2&ab_channel=CodingMadeEasy
Okay I got my sort to work..but not I cant get my sum to work lol it keeps giving me totally random numbers with letters in it. Heres my code
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 
	}
}
1
2
Sum = sum(salary, totalRecs);
cout << "Sum = " << sum << endl;


Your sum is called Sum. With a uppercase S. not sum, with a lowercase S.

Edit: I would recommend you learn how to debug. Debugging is in my opinion equally important as writing code. Its a must-know. And will save you lots of trouble and time wasted.
Last edited on
thanks, and yeah I read through it, but I'm just not good at spotting out little things like that yet
Debugging does not mean reading through your code. Not even close.

https://www.youtube.com/playlist?list=PLHJE4y54mpC4lnCrwbEssAf9e-A9U4el4

These 6 videos (although not c++, its c# but that doesnt matter) Is a good series that'll teach you debugging.
Topic archived. No new replies allowed.