Insertion sort reading from file into structure

I was tasked to write a program that will read the data from a txt file into a structure. i then have to prompt for what i want the user for input on what they want to sort by. the input would then determine what columns will be sorted. I am not having any issues sorting the numbers in the file. the issue i am having is with the names. i need to sort them alphabetically when prompted. I know the code i have so far works for a bubble sort although i am not aloud to use it. in my case statements below you will see how i was able to get the bubble sort to work for case 1: but if you compile case 2: there will be errors. all other cases seem to work just fine. please note i have not taken the time to clean the code up to make it look nice but i did try my best to comment important parts.
Please any help at all is much appreciated, I really want to get this to work.
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <windows.h>
#include <conio.h>
#include <vector>

using namespace std;

// constants
const int MAX_EMPLOYEE = 100;
const string DEPARTMENT_NAMES [] = {"Corporate","Sales","Marketing","Technology","Support"};
const string HEADINGS[] = {"EmpNum", "First", "Last", "Dept", "Salary"};
const string HEADINGS_ABBREV = "eflds";

struct Employee 
{
	int employeeNum;
	int department;
	string firstName;
	string lastName;
	double salary;
}employee[MAX_EMPLOYEE];

//Prototypes
//int workerFile();
int getIndex(string prompt, bool allowEmpty, bool allowOutside, bool ignoreCase,  string choices);
char getChar(string prompt, bool allowEmpty);
void sortEmployee(Employee employee[MAX_EMPLOYEE], int value, int value2);
void printHeading(Employee employee[MAX_EMPLOYEE], int value);

int main(int argc, char *argv[]) 
{
	int i = 0;
	int j = 0;

	int numEmployee = 0;
	Employee employee[MAX_EMPLOYEE];
	int sortInput = 0;


	ifstream employeetxt;
	employeetxt.open("employee.dat");

	while (employeetxt)
	{
		employeetxt >> employee[numEmployee].employeeNum 
					>> employee[numEmployee].firstName
					>> employee[numEmployee].lastName 
					>> employee[numEmployee].department 
					>> employee[numEmployee].salary; 

		numEmployee++;
	}

	employeetxt.close();
	numEmployee--;

	cout << setprecision(2) << fixed;
	cout << "There are " << numEmployee << " employees.\n" << endl;

	while (true)
	{
		//Prompt for which field to sort by
		cout << "\nHow would you like them displayed?" << endl;
		sortInput = getIndex("(by Emp Num, First name, Last name, Dept, Salary or <Enter to quit>) ", true, false, true, HEADINGS_ABBREV);
		if (sortInput == -1)
			break;
		sortEmployee(employee, numEmployee, sortInput);	
		printHeading(employee, numEmployee);
	} 
	cout << "\n\n";
	system("pause");
}


void printHeading(Employee employee[MAX_EMPLOYEE], int value)
{
		cout << "\n" << setw(8) << "";
		for (int i = 0; i < 5; i++)
		{
			cout << HEADINGS [i] << setw(12);
		}
		cout << endl;
		for (int i = 0; i < value; i++)
		{
			cout << setw(2) << (i+1) << setw(12) << employee[i].employeeNum 
						  << setw(12) << employee[i].firstName 
						  << setw(12) << employee[i].lastName 
						  << setw(12) << DEPARTMENT_NAMES [employee[i].department] 
				          << setw(12) << employee[i].salary << endl;
		}
}

void sortEmployee(Employee employee[MAX_EMPLOYEE], int value, int value2)
{
	int i, j;
	int startScan, minIndex, minValue = 0;

	switch(value2)
	{
		case 0: //insertion sort on the int values no issues
			startScan, minIndex, minValue = 0;
			for (startScan = 0; startScan < (value - 1); startScan++) 
			{
				minIndex = startScan;
				minValue = employee[startScan].employeeNum;
				for (int index = startScan + 1; index < value; index++)
				{
					if (employee[index].employeeNum < minValue)
					{
						minValue = employee[index].employeeNum;
						minIndex = index;
					}
				} 
				employee[minIndex].employeeNum = employee[startScan].employeeNum;
				employee[startScan].employeeNum = minValue;
			}
			break;
		case 1: //bubble sort works with no problems
			startScan, minIndex, minValue = 0;
			for (i = 0; i < value-1; i++) 
			{
				for (j = i+1; j < value; j++) 
				{
					if (employee[i].firstName > employee[j].firstName) 
					{
						swap(employee[i],employee[j]);
					}
				}
			}
			break;
		case 2: //attempt to use insertion sort on strings(Does Not Work)
			startScan, minIndex, minValue = 0;
			for (startScan = 0; startScan < (value - 1); startScan++) 
			{
				minIndex = startScan;
				minValue = employee[startScan].lastName;
				for (int index = startScan + 1; index < value; index++)
				{
					if (employee[index].lastName < minValue)
					{
						minValue = employee[index].lastName;
						minIndex = index;
					}
				} 
				employee[minIndex].lastName = employee[startScan].lastName;
				employee[startScan].lastName = minValue;
			}
			break;
		case 3:  //insertion sort on the int values no issues (Note these are organized 
			startScan, minIndex, minValue = 0;
			for (startScan = 0; startScan < (value - 1); startScan++) 
			{
				minIndex = startScan;
				minValue = employee[startScan].department;
				for (int index = startScan + 1; index < value; index++)
				{
					if (employee[index].department < minValue)
					{
						minValue = employee[index].department;
						minIndex = index;
					}
				} 
				employee[minIndex].department = employee[startScan].department;
				employee[startScan].department = minValue;
			}
			break;
		case 4:
			startScan, minIndex, minValue = 0;
			for (startScan = 0; startScan < (value - 1); startScan++) 
			{
				minIndex = startScan;
				minValue = employee[startScan].salary;
				for (int index = startScan + 1; index < value; index++)
				{
					if (employee[index].salary < minValue)
					{
						minValue = employee[index].salary;
						minIndex = index;
					}
				} 
				employee[minIndex].salary = employee[startScan].salary;
				employee[startScan].salary = minValue;
			}
			break;
		default:
			break;
	}
}

int getIndex(string prompt, bool allowEmpty, bool allowOutside, bool ignoreCase,  string choices) {

	int index = -1; // value to be returned;

	do {
		char c = getChar(prompt, allowEmpty);
		if (c == 0 && allowEmpty)
			break;

		if (ignoreCase) {
			c = toupper(c);
		}

		index = choices.find(c,0);
		if (index < 0 && ignoreCase) {
			index = choices.find(tolower(c),0);
		}
		
		if (index >= 0 || allowOutside)
			break;

		cout << "You must enter one of these characters '" << choices << "'\n";
	} while (true);

	return index;
}

char getChar(string prompt, bool allowEmpty) {
	char c = 0;
    string buffer;

	do {
		cout << prompt;
		getline(cin, buffer);
		if (buffer.length() > 0) {
			c = buffer.at(0);
			break;
		} else if (allowEmpty)
			break;
		cout << "You must enter at least one character.\n";
	} while (true);
	return c;
}
What exactly goes wrong when you run the program in case 2? I think problem might be related to the way strings behave.
here are the errors that i receive when i try to build. in case 1: it builds just fine no errors. case 2: will allow the program to run but will not respond to the sort algorithm.
also note in case 3: the algorithm will only sort the position values from the file (employee[index].department) and not the values from DEPARTMENT_NAMES []




1>c:\*\employee\employee\main.cpp(169): error C2676: binary '<' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\*\employee\employee\main.cpp(171): error C2440: '=' : cannot convert from 'std::string' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Ah, I see...
Ok, instead of using comparing operators > and <, use strcmp function.
strcmp(string1,string2)
will return negative value if string1 is lexicographically before string2,
will return zero if strings are identical,
will return positive value if string2 is lexicographically before string1.

Hope this will do it :) Good luck!
Topic archived. No new replies allowed.