Help with Data Structures and Sorting and Searching

Ok I am working on a project that says to write a program that inputs (at most 50) records from a sequential file. The file may contain fewer than 50 records. Then insert the records into an array of records (ordered by department code in alphabetical order). Last, print the records in alphabetical order by department code and compute the age for each employee.

This is my data file (sampledata.txt)
Its last name, first name, date of birth, salary, dept #, phone #

Smith Tom 03141989 59150.00 B 253-760-5811
Hall Debra 05061980 61500.00 C 253-850-6880
Gaines Sarah 11241958 65500.00 d 397-196-1420
Ritz Robert 03301970 80250.00 a 385-300-4068
Martin Kevin 07151990 75000.00 A 397-123-2410
Holloway Jason 12181985 72750.00 a 385-697-9713
Hoan Kimberly 05161991 53125.00 b 253-156-5195
Bryan Victor 01231989 41150.00 D 407-701-7022
Roberts Ben 01121989 55000.00 c 253-519-1818
Little Mary 01181991 40685.00 d 860-714-2039
Samson Rachel 02021981 70515.00 A 305-312-0600
Peach Mel 10301965 60016.00 c 397-142-4061
Carrs Brett 04261969 56250.00 B 253-804-2000
Walsh Edward 09211993 49080.00 d 204-241-3033
Austin Jacob 08131992 48130.00 D 385-402-5741

This is my code so far...
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

//structure declaration
struct employee_struct
{
	char last_name[15];
	char first_name[15];
	char date_of_birth[8]; //mmddyyyy
	float annual_salary;
	int age;
	char dept_code;
	char phone_number[12];
};

int main()
{
	employee_struct employee;

	const int size=15; //I have 15 records in my data file
	double array[size];
	int i = 0;

	//open file
	fstream inFile;
	inFile.open("sampledata.txt");

	//read data into array of records
	for (i = 0; i < 50; i++)
	{
		input >> employee[i].last_name >> employee[i].first_name >> employee[i].date_of_birth >> employee[i].annual_salary >> employee[i].age >> employee[i].dept_code >> employee[i].phone_number;
	}

	//close file
	inFile.close();

	//sort array of records by dept_code
	

	return 0;
}


PLEASE HELP!!! I'm just learning data structures and methods of sorting and searching. Could someone please give my code a look to see if I'm even starting correctly. I attempted the sorting but it wasn't going so well and I keep getting errors at line 41.
Last edited on
First problem I see is line 29, I believe it should be ifstream. Im not completely sure if that is the only problem but try it first.
Also make sure the .txt file you are reading from is in the same directory as the .cpp file and in your for loop why input? if your filling a struct from a file you should be using the same ifstream variable as the one you created, inFile.
Last edited on
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <algorithm>

using namespace std;

//structure declaration
struct employee_struct
{
	char last_name[15];
	char first_name[15];
	char date_of_birth[8]; //mmddyyyy
	float annual_salary;
	int age;
	char dept_code;
	char phone_number[12];
};

int main()
{
	employee_struct employee[15];

	const int size=15;
	double array[size];
	int i = 0;

	//open file
	ifstream inFile;
	inFile.open("sampledata.txt");

	//read data into array of records
	for (i = 0; i < 50; i++)
	{
		inFile >> employee[i].last_name >> employee[i].first_name >> employee[i].date_of_birth >> employee[i].annual_salary >> employee[i].age >> employee[i].dept_code >> employee[i].phone_number;
                employee[i].dept_code=toupper(employee[i].dept_code);
	}

	//close file
	inFile.close();

	//sort array of records by dept_code HELP!!!
	

	return 0;
}


Thanks so much @Aim4Erudite, your suggestions got rid of my errors. I understand what I did wrong there.
I'm having trouble with sorting my structure array in alphabetical order. Do I need to do another conversion of the dept_code member to numbers to put them in order or can I just sort them while they are letters?
Last edited on
Have a look at the integer values corresponding to ASCII characters: http://web.cs.mun.ca/~michael/c/ascii-table.html

Notice that letters further down in the alphabet have a higher integer value. That should help you with comparing characters to each other. Also notice that some of the department codes are uppercase, and uppercase chars all correspond to lower integer values than lowercase ones. Consider converting all of them to uppercase using this function: http://www.cplusplus.com/reference/cctype/toupper/
Topic archived. No new replies allowed.