Sorting Strings alphabetically

Pages: 12
Hello! I am working on a project for class, and one of the things we need to do is sort based off of a string.

We defined our own class as follows:

1
2
3
4
5
6
7
8
9
class Employee
{
        private:
            int id;
            char *fName;
            char *lName;
            double wage;
            double rate;
            int hours;


etc. and we are supposed to write a sorting function from our second class:

1
2
3
4
5
class EmployeeList
{
        private:
                Employee workers[MAX_SIZE];
                int count;


We are supposed to sort by last name, then by first name. I don't recall how to sort strings alphabetically...but this is what I was thinking of doing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        EmployeeList EmployeeList::sortName ()
        {
                bool status = true;
                Employee temp;

                if (status)
                {
                status = false;

                        for (int i = 0; i < count - 1; i++)
                        {
                                if (workers[i].getlName > workers[i+1].getlName)
                                {
                                        temp = workers[i+1];
                                        workers[i+1] = workers[i];
                                        workers[i] = temp;
                                        status = true;
                                }
                        }
                }

                return EmployeeList;
        }


Now I know you can't compare with >,< with strings, unless you use atoi or something similar, but I would like to do something similar to this bubble sort.

Also, not sure how to include if the lnames are equal, use first names, perhaps another couple of if statements?

Lastly, not sure if my class declarations are correct, but I want to work on that after I finish coding everything else.

If you need some more snippets of code from my classes, let me know.

Thanks for your help,
Soko
Last edited on
If you use std::strings instead of char arrays, you will be able of using the < operator http://www.cplusplus.com/reference/string/operators/
If not, you can use strcmp: http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
http://en.wikipedia.org/wiki/Selection_sort

has some great c and java examples. (how they got in there is beyond me, someone will probably delete them soon enough)

reining in on bazzy's parade there's an easy swap for strings as well which makes doing the temp=i, i=j, j=temp redundant. strings are just nice in general :)
Last edited on
The reason I don't like selection sort is because we did a project on that earlier in the year. For some reason my selection sort code was not working, and was getting me very frustrated, whereas my bubblesort code was easy to do. So I prefer to use that, even if bubble sort is way less efficient. I know in the future I will need to learn to do selection sort though...

And thanks for the help, I am sure strcmp will work. I will keep this thread open just in case I have a problem with this program when I finish writing it.
My Employee.cpp is:
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
//Implements Employee.h
#include "Employee.h"
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

/****************************************************
Constructor
****************************************************/

	void Employee::EmployeeInfo(char *name1, char *name2, int ident, double payRate, int hoursWorked)
	{
		fName = new char[strlen(name1)+1];
		strcpy (fName, name1);
		lName = new char[strlen(name2)+1];
		strcpy (lName, name2);
		id = ident;
		rate = payRate;
		hours = hoursWorked;
	};

/***************************************************
This function defines the == operator
***************************************************/

	    	bool Employee &operator==(Employee &right) //Line 29
	    	{
	    		for (int count = 0; count < 30; count++)
	    		{
				if (fName[count] != right.getFName[count])
				return false;
				else if (lName[count] != right.getLName[count])
				return false;
	    		} 
	    		if (id != right.getId &right)
	    		return false;
	    		else if (wage != right.wage)
	    		return false;
	    		else if (rate != right.rate)
	    		return false;
	    		else if (hours != right.hours)
	    		return false;
	    		else
	    		return true;
	    	}


my Employee.h is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <iostream>
using namespace std;

class Employee
{
	private:
		int id;
		char *fName;
		char *lName;
		double wage;
		double rate;
		int hours;
       public:
                ...
		bool operator == (Employee&);


and for my errors I get:

> g++ -c Employee.cpp
Employee.cpp:29: error: expected init-declarator before '&' token
Employee.cpp:29: error: expected `,' or `;' before '&' token

Any help is appreciated, thanks!

Soko
bool Employee &operator==(Employee &right) //Line 29

Shouldn't that be

bool Employee::operator==(...)
if I replace that, I get the error:

Employee.cpp: In member function `bool Employee::operator==(Employee&)':
Employee.cpp:33: error: invalid types `<unknown type>[int]' for array subscript
Employee.cpp:35: error: invalid types `<unknown type>[int]' for array subscript
Employee.cpp:38: error: invalid use of member (did you forget the `&' ?)


Then I try to add
 
bool Employee::&operator==(Employee &right) //Line 29 


and get:

Employee.cpp:29: error: expected unqualified-id before '&' token
Employee.cpp:29: error: expected init-declarator before '&' token
Employee.cpp:29: error: expected `,' or `;' before '&' token


Which does nothing to help the error and just adds another. So I don't think that was it.
Segmentation fault! Finally got it compiled though...
Employee.h:
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
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <iostream>
using namespace std;

class Employee
{
        private:
                int id;
                char *fName;
                char *lName;
                double wage;
                double rate;
                int hours;
        public:
                void EmployeeInfo(char*, char*, int, double, int);

                //Mutators
                void setId(int ident)
                {
                        id = ident;
                };

                void setFName(char *name1)
                {
                        *fName = *name1;
                };

                void setLName(char *name2)
                {
                        *lName = *name2;
                };

                void setWage(double r, int h)
                {
                        wage = h * r;
                };

                void setRate(double payRate)
                {
                        rate = payRate;
                };

                void setHours(int hoursWorked)
                {
                        hours = hoursWorked;
                };

                //Fetchers
                int getId()
                {
                        return id;
                };

                char* getFName()
                {
                        return fName;
                };

                char* getLName()
                {
                        return lName;
                };

                double getWage()
                {
                        wage = rate * hours;
                        return wage;
                };

                double getRate()
                {
                        return rate;
                };

                int getHours()
                {
                        return hours;
                };

                bool operator == (Employee&);

                bool operator != (Employee&);

                friend ostream &operator << (ostream &, Employee &);

                friend istream &operator >> (istream &, Employee &);

};

#endif 


And here is Employee.cpp:
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
//Implements Employee.h
#include "Employee.h"
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

/****************************************************
Constructor
****************************************************/

        void Employee::EmployeeInfo(char *name1, char *name2, int ident, double payRate, int hoursWorked)
        {
                fName = new char[strlen(name1)+1];
                strcpy (name1, fName);
                lName = new char[strlen(name2)+1];
                strcpy (name2, lName);
                id = ident;
                rate = payRate;
                hours = hoursWorked;
        };

/***************************************************
This function defines the == operator
***************************************************/

        bool Employee:: operator == (Employee &obj2)
        {
                int z;
                z = strcmp(fName, obj2.fName);
                if (z != 0)
                        return false;
                z = strcmp(lName, obj2.lName);
                if (z != 0)
                        return false;
                if (id != obj2.id)
                        return false;
                if (wage != obj2.wage)
                        return false;
                if (rate != obj2.rate)
                        return false;
                if (hours != obj2.hours)
                        return false;
                return true;
        }

/**************************************************
This function defines the != operator
**************************************************/

        bool Employee:: operator != (Employee &obj2)
        {
                bool status = false;
                for (int count = 0; count < 30; count++)
                {
                        if (fName != obj2.fName)
                        {
                                status = true;
                                break;
                        }
                }
                if (!status)
                        {if (id != obj2.id)
                                {if (wage != obj2.wage)
                                        {if (rate != obj2.rate)
                                                {if (hours != obj2.hours)
                                                        {
                                                                return true;
                                                        }
                                                }
                                        }
                                }
                        }
                else
                return false;
        }

/****************************************************
These next two functions define the << and >> for the Employee class
****************************************************/

            ostream &operator << (ostream &strm, Employee &obj)
            {
                strm << setw(10) << setprecision(0) << obj.id << setw(20) << obj.fName << setw(20) << obj.lName;
                strm << setw(10) << setprecision(2) << obj.rate;
                strm << setw(10) << setprecision(0) << obj.hours << setw(10) << setprecision(2) << obj.wage << endl;
                return strm;
            }

            istream &operator >> (istream &strm, Employee &obj)
            {
                strm >> obj.id >> obj.fName >> obj.lName >> obj.rate >> obj.hours;
                return strm;
            }

            istream &operator >> (istream &strm, Employee &obj)
            {
                strm >> obj.id >> obj.fName >> obj.lName >> obj.rate >> obj.hours;
                return strm;
            }


Errors:
csci2>g++ Employee.cpp
Undefined first referenced
symbol in file
main /usr/lib/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status


Any help is much appreciated, thanks!!!

EDIT: Solved, problem was the compilation. g++ Employee.cpp was looking for an int main() function to start at, and there is no int main because it is part of a class definition. Solution:
type g++ -c Employee.cpp. This tells the compiler that you are not trying to create an output function with it.

Onto the next set of classes and errors fixing.
Last edited on
Ok now stuck on set two of classes:

EmployeeList.h
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
#ifndef EMPLOYEELIST_H
#define EMPLOYEELIST_H

#include <iostream>
#include "Employee.h"
using namespace std;

const int MAX_SIZE = 50;

class EmployeeList
{
        private:
                Employee workers[MAX_SIZE];
                int count;
        public:
                void EmployeeListInfo(Employee EmployeeInfo, int count)
                {
                        workers[count] = EmployeeInfo;
                        count++;
                };

                Employee* employeeHolder(string);

                void addEmp(Employee &);

                void delEmp(Employee &);

                int searchEmp(int);

                Employee* sortId();

                Employee* sortName();

                void modEmp(int);

                int numEmp();

                Employee retEmp(int);

                friend ostream &operator << (ostream &, EmployeeList &);

                friend istream &operator >> (ostream &, EmployeeList &);
                void writeEmp();
};

#endif 


And EmployeeList.cpp:
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
//Implements EmployeeList.h
#include "EmployeeList.h"
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

/***********************************************************
Constructor included in EmployeeList.h
***********************************************************/

/**********************************************************
Fetches from the file Employeedata.dat
**********************************************************/

        Employee* EmployeeList::employeeHolder(string fileName)
        {

                ifstream inFile;
                inFile.open(fileName);

                while (inFile.good())
                {
                        inFile >> workers[count];
                        count++;
                }
                count--;
                inFile.close();

                writeEmp();
                return workers;
        }

/************************************************************
This function adds employees to the list
************************************************************/

        void EmployeeList::addEmp(Employee &aWorker)
        {
                //To check if conditions are met to add an employee
                bool status = true;

                if (count + 1 == MAX_SIZE)
                {
                        cout << "Database is full.  Please delete an employee or contact your server admin." << endl;
                        status = false;
                }

                if (status)
                {
                        for (int i=0; i < count; i++)
                        {
                                if (workers[i] == aWorker)
                                {
                                        cout << "Error, employee is already in database." << endl;
                                        status = false;
                                }
                        }
                }

                if (status)
                {
                        workers[count] = aWorker;
                        count++;
                }
                writeEmp();
        }

/***************************************************************
This function deletes an employee from the database
***************************************************************/

        void EmployeeList::delEmp(Employee &aWorker)
        {
                //To check if conditions are met to del an employee
                bool status = true;

                if (count == 0)
                {
                        cout << "Database is empty.  There must be an existing employee to delete one." << endl;
                        status = false;
                }


                if (status)
                {
                        for (int i=0; i < count; i++)
                        {
                                if (workers[i] == aWorker)
                                {
                                        if (count == 1)
                                                count--;
                                        else
                                        {
                                                workers[i] = workers[count - 1];
                                                count--;
                                        }

                                        cout << "Worker deleted successfully." << endl;
                                }
                                else
                                {
                                        cout << "Worker in database does not match the criteria provided." << endl;
                                }
                        }
                }
                writeEmp();
        }

/*********************************************************************
This function searches for an employee and returns the index in the array
**********************************************************************/

        int EmployeeList::searchEmp(int ident)
        {

                for (int i=0; i < count; i++)
                {

                        if (workers[i].id == ident)
                        {
                                return i;
                        }
                }

                writeEmp();
                return -1;
        }

/***********************************************************************
This function sorts the list by ID and returns the list
***********************************************************************/

        Employee* EmployeeList::sortId()
        {
                bool status = true;
                Employee temp;

                if (status)
                {
                status = false;

                        for (int i = 0; i < count - 1; i++)
                        {
                                if (workers[i].id > workers[i+1].id)
                                {
                                        temp = workers[i+1];
                                        workers[i+1] = workers[i];
                                        workers[i] = temp;
                                        status = true;
                                }
                        }
                }

                writeEmp();
                return workers;
        }

/**********************************************************************
This function sorts the list by name and returns the list
**********************************************************************/

        Employee* EmployeeList::sortName()
        {
                bool status = true;
                Employee temp;
                int z = 0;

                if (status)
                {
                status = false;

                        for (int i = 0; i < count - 1; i++)
                        {
                                z = strcmp(workers[i].lName, workers[i+1].lName);
                                if (z > 0)
                                {
                                        temp = workers[i];
                                        workers[i] = workers[i+1];
                                        workers[i+1] = temp;
                                        status = true;
                                }
                                if (z == 0)
                                {
                                        z = strcmp(workers[i].fName, workers[i+1].fName);
                                        if (z > 0)
                                        {
                                                temp = workers[i];

                                                workers[i] = workers[i+1];
                                                workers[i+1] = temp;
                                                status = true;
                                        }
                                }
                        }
                }

                writeEmp();
                return workers;
        }
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

/***********************************************************************
This function modifies an employee's info
***********************************************************************/

        void EmployeeList::modEmp(int i)
        {
                int mod = 0;
                double mod1 = 0;
                char name[30];
                char choice;

                cout << "Would you like to modify the employee's first name?" << endl;
                cin >> choice;

                if (choice == 'Y' || choice == 'y')
                {
                        cout << "Please enter the new employee's first name." << endl;
                        cin >> name;

                        workers[i].setFName(name);
                }


                cout << "Would you like to modify the employee's last name?" << endl;
                cin >> choice;

                if (choice == 'Y' || choice == 'y')
                {
                        cout << "Please enter the new employee's last name." << endl;
                        cin >> name;


                        workers[i].setLName(name);
                }

                cout << "Would you like to modify the employee's pay rate?" << endl;
                cin >> choice;

                if (choice == 'Y' || choice == 'y')
                {
                        cout << "Please enter the new employee's pay rate." << endl;
                        cin >> mod1;

                        workers[i].setRate(mod1);
                        workers[i].setWage(workers[i].getRate(), workers[i].getHours());
                }

                cout << "Would you like to modify the employee's hours?" << endl;
                cin >> choice;

                if (choice == 'Y' || choice == 'y')
                {
                        cout << "Please enter the new employee's hours." << endl;
                        cin >> mod;

                        workers[i].setHours(mod);
                        workers[i].setWage(workers[i].getRate(), workers[i].getHours());
                }

                writeEmp();
        }

/***********************************************************************
This function returns how many employees there currently are
***********************************************************************/

        int EmployeeList::numEmp()
        {
                return count;
        }

/***********************************************************************
This function returns a single employee
***********************************************************************/

        Employee EmployeeList::retEmp(int i)
        {
                return workers[i];
        }

/************************************************************************
These next two functions define the << and >> operators for EmployeeList
************************************************************************/

        ostream &operator << (ostream &strm, EmployeeList &obj)
        {
                for (int i = 0; i < count; i++)  //Line 287
                {
                        strm << workers[i]; //Line 289
                }

                return strm;
        }

        istream &operator >> (istream &strm, EmployeeList &obj)
        { //Line 296
                workers[count] = obj; //Line 297
                return strm;
        }

/******************************************************************
This function writes workers to output file
******************************************************************/

        void EmployeeList::writeEmp()
        {
                ostream outFile; //Line 307
                outFile.open("EmployeeDataBase.dat");  //Line 308

                for (int i = 0; i < count; i++)
                {
                        outFile >> workers[i]; //Line 312
                }

                outFile.close(); //Line 315
        }


Compilation errors:
csci2>g++ -c EmployeeList.cpp
EmployeeList.cpp: In member function `Employee* EmployeeList::employeeHolder(std::string)':
EmployeeList.cpp:22: error: no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'
/usr/sfw/lib/gcc/i386-pc-solaris2.10/3.4.3/../../../../include/c++/3.4.3/fstream:570: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
EmployeeList.cpp: In function `std::ostream& operator<<(std::ostream&, EmployeeList&)':
EmployeeList.cpp:287: error: invalid operands of types `int' and `<unknown type>' to binary `operator<'
EmployeeList.cpp:289: error: `workers' undeclared (first use this function)
EmployeeList.cpp:289: error: (Each undeclared identifier is reported only once for each function it appears in.)
EmployeeList.cpp: In function `std::istream& operator>>(std::istream&, EmployeeList&)':
EmployeeList.cpp:297: error: `workers' undeclared (first use this function)
EmployeeList.cpp: In member function `void EmployeeList::writeEmp()':
/usr/sfw/lib/gcc/i386-pc-solaris2.10/3.4.3/../../../../include/c++/3.4.3/ostream:361: error: `std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char, _Traits = std::char_traits<char>]' is protected
EmployeeList.cpp:307: error: within this context
EmployeeList.cpp:308: error: 'struct std::ostream' has no member named 'open'
EmployeeList.cpp:312: error: no match for 'operator>>' in 'outFile >> ((EmployeeList*)this)->EmployeeList::workers[i]'
Employee.h:88: note: candidates are: std::istream& operator>>(std::istream&, Employee&)
EmployeeList.h:42: note: std::istream& operator>>(std::ostream&, EmployeeList&)
EmployeeList.cpp:296: note: std::istream& operator>>(std::istream&, EmployeeList&)
EmployeeList.cpp:315: error: 'struct std::ostream' has no member named 'close'


Now what I am confused about each error is as follows:
Line 22: I have seen that error before, bt only with user defined functions (not ifstream declerations)
The rest: a combination of not being declared (even though it is declared in EmployeeList.h) and for some reason other little things (like seeming not to be including the fstream library even though it is included on top).

Any help is, as always, much appreciated. :)

EDIT: Labeled all lines which have errors in the second part (I think) since I couldn't fit it all in one post.
Last edited on
EmployeeList.cpp:
Part1, Line 22, ifstream::open takes a C string, add .c_str() after fileName
Part 2, line 86: You are using class members but the << operator is external to the class so you must specify the object
( same for >> )
Part 2, line 108: you are declaring an ostream but you want an ofstream

Part1, Line 22, ifstream::open takes a C string, add .c_str() after fileName
Part 2, line 86: You are using class members but the << operator is external to the class so you must specify the object
( same for >> )
Part 2, line 108: you are declaring an ostream but you want an ofstream


#1,3 fixed. Easy mistakes :D and I don't know how many times I looked over number two and missed that. Sometimes it just takes a fresh pair of eyes I guess.

Question on #2: By specifying the object, do you mean:
 
        ostream &operator << (ostream &strm, EmployeeList &right)

? Specifying the object in that way? Or:

 
       ostream EmployeeList &operator << (ostream &strm, EmployeeList &right)

?


And left for errors now:
csci2>g++ -c EmployeeList.cpp
EmployeeList.cpp: In function `std::ostream& operator<<(std::ostream&, EmployeeList&)':
EmployeeList.cpp:287: error: invalid operands of types `int' and `<unknown type>' to binary `operator<'
EmployeeList.cpp:289: error: `workers' undeclared (first use this function)
EmployeeList.cpp:289: error: (Each undeclared identifier is reported only once for each function it appears in.)
EmployeeList.cpp: In function `std::istream& operator>>(std::istream&, EmployeeList&)':
EmployeeList.cpp:297: error: `workers' undeclared (first use this function)


Which I am guessing will be fixed by specifying the object.
Add right. before the members used inside the function
Hmmm, now for errors I am getting:

csci2>g++ -c EmployeeList.cpp
EmployeeList.cpp: In function `std::ostream& operator<<(std::ostream&, EmployeeList&)':
EmployeeList.cpp:287: error: invalid operands of types `int' and `<unknown type>' to binary `operator<'
EmployeeList.cpp: In function `std::istream& operator>>(std::istream&, EmployeeList&)':
EmployeeList.cpp:299: error: `workers' undeclared (first use this function)
EmployeeList.cpp:299: error: (Each undeclared identifier is reported only once for each function it appears in.)
EmployeeList.cpp:299: error: 'class EmployeeList' has no member named 'Employee'


and my code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        ostream &operator << (ostream &strm, EmployeeList &right)
        {
                for (int i = 0; i < count; i++) //Line 287
                {
                        strm << right.workers[i];
                        cout << endl;
                }

                return strm;
        }

        istream &operator >> (istream &strm, EmployeeList &right)
        {

                workers[count] = right.Employee; //Line 299
                return strm;
        }
count is a member too, line 299 doesn't make much sense ( you are never using the istream )

BTW, you can use [code firstline=286][/code]
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
	ostream & operator << (ostream &strm, EmployeeList &right)
	{
		for (int i = 0; i < count; i++)
		{
			strm << right.workers[i];
			cout << endl;
		}

		return strm;
	}

	istream & operator >> (istream &strm, EmployeeList &obj2)
	{

		strm >> obj2.Employee;
		return strm;
	}


> g++ -c EmployeeList.cpp
EmployeeList.cpp: In function `std::ostream& operator<<(std::ostream&, EmployeeList&)':
EmployeeList.cpp:287: error: invalid operands of types `int' and `<unknown type>' to binary `operator<'
EmployeeList.cpp: In function `std::istream& operator>>(std::istream&, EmployeeList&)':
EmployeeList.cpp:299: error: 'class EmployeeList' has no member named 'Employee'

Now when you say it is a member, that means it should be automatically be useable by this function. It is declared as a private member, but this is part of my file where it is designated as a private member, so I should be able to access it, and I shouldn't have to initialize it or anything, if I understand classes somewhat...

and for line 299, it is essentially the same, except Employee is not a private member function. And I don't need to do anything special to make reference to these parameters in any other function.
<< and >> aren't members, they are friends
You are still missing right. before count on line 287, you should have strm instead of coun on line 290.
In >> you want to read obj2.workers (which is an array)
Ahhh the only reason I linked the selection sort is it's usually the easiest to grasp. You don't really need to use it there's many better sorts out there. eg. Insertion, bi-directional bubble sort, Quick sort, LargeStorage Merge sort. etc. etc. Depends on your use, quick sort and insertion are good for smaller arrays, and insertion is even better at already sorted arrays, so for arrays that see continued use throught life of program and are sorted and added to regularly insertion is probably the best. Selection sort can be one of the slowest to use.
Because for each element in the array is the amount of times the sort needs to traverse it. Find smallest put swap with 0, find smallest swap with 1, find smallest swap with 2, so on...

whereas insertion grabs element 1, puts it in the correct position prior to the position(so checks against 0, not every element) grabs 2, checks against 0&1, grabs 3, checks with 0,1,2 and so on... So for already sorted arrays that may have a couple of changes to elements this is extremely fast.
Last edited on
Hmmm well this isn't good.

Errors:
csci2>g++ Employee.cpp EmployeeList.cpp payRoll.cpp
Undefined first referenced
symbol in file
deleteEmployees(EmployeeList) /var/tmp//ccbnbpMe.o
inputEmployeeData(EmployeeList) /var/tmp//ccbnbpMe.o
modifyEmployeeData(EmployeeList) /var/tmp//ccbnbpMe.o
addNewEmployees(EmployeeList) /var/tmp//ccbnbpMe.o
displayEmployeeData(EmployeeList) /var/tmp//ccbnbpMe.o
sortAndDisplayEmployeeData(EmployeeList) /var/tmp//ccbnbpMe.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <iostream>
#include <iomanip>
#include "Employee.h"
#include "EmployeeList.h"

using namespace std;

//Prototype decleration
void inputEmployeeData(EmployeeList);
void addNewEmployees(EmployeeList);
void modifyEmployeeData(EmployeeList);
void deleteEmployees(EmployeeList);
void displayEmployeeData(EmployeeList);
void sortAndDisplayEmployeeData(EmployeeList);

int main()
{

        EmployeeList worker[MAX_SIZE];

        int choice = 0;

        do
        {
        cout << "***************************************************************" << endl;
        cout << "***************************************************************" << endl;
        cout << "**                             Menu                          **" << endl;
        cout << "**1. Create an employee database.                            **" << endl;
        cout << "**2. Add new employee to the database.                       **" << endl;
        cout << "**3. Modify an employee's data.                              **" << endl;
        cout << "**4. Delete an employee from the database.                   **" << endl;
        cout << "**5. Display all employee's in database to the screen.       **" << endl;
        cout << "**6. Sort and display an employee's data to the screen.      **" << endl;
        cout << "**7. Quit the system.                                        **" << endl;
        cout << "***************************************************************" << endl;
        cout << "***************************************************************" << endl;

        cout << "\n Please enter a choice from the menu." << endl;
        cin >> choice;

        if (choice == 1)
        {
                inputEmployeeData(worker[MAX_SIZE]);
        }

        if (choice == 2)
        {
                addNewEmployees(worker[MAX_SIZE]);
        }

        if (choice == 3)
        {
                modifyEmployeeData(worker[MAX_SIZE]);
        }

        if (choice == 4)
        {
                deleteEmployees(worker[MAX_SIZE]);
        }

        if (choice == 5)
        {
                displayEmployeeData(worker[MAX_SIZE]);
        }

        if (choice == 6)
        {
                sortAndDisplayEmployeeData(worker[MAX_SIZE]);
        }

        }
        while (choice != 7);

        cout << "Thanks for using the employee database system." << endl;
        cout << "Have a nice day!" << endl;

        return 0;
}

void inputEmployeeData(EmployeeList worker[MAX_SIZE])
{
        char choice;
        Employee temp;
        bool status = false;
        int count;

        do
        {
        cout << "\nPlease enter the employee info in the following order: ID, First Name, Last Name,";
        cout << "Pay Rate, and Hours Worked (with spaces, without the commas)." << endl;
        cin >> temp;

        count = worker[count].numEmp();

        if (!status)
        {
        worker[count].EmployeeListInfo(temp, count);
        status = true;
        }

        if (status)
        {
        worker[count].addEmp(temp);
        }

        cout << "Would you like to add another employee? (Y/N)" << endl;
        cin >> choice;
        }
        while (choice == 'Y' || choice == 'y');

        cout << "Database has been created." << endl;
}

void addNewEmployees(EmployeeList worker[MAX_SIZE])
{
        Employee temp;
        char choice;
        int i = 0, count;

        do
        {

        cout << "\nPlease enter the employee info in the following order: ID, First Name, Last Name," << endl;
        cout << "Pay Rate, and Hours Worked (with spaces, without the commas)." << endl;
        cin >> temp;

        count = worker[i].numEmp();

        worker[i].addEmp(temp);

        cout << "Would you like to add another employee? (Y/N)" << endl;
        cin >> choice;
        }
        while (choice == 'y' || choice == 'Y');

        cout << "All employees added." << endl;
}

void modifyEmployeeData(EmployeeList worker[MAX_SIZE])
{
        int id, i, j = 0, count;
        char choice;

        do
        {
        cout << "\nPlease enter the employee's ID" << endl;
        cin >> id;

        count = worker[j].numEmp();

        i = worker[j].searchEmp(id);
        if (i == -1)
        {
                cout << "No employee exists with that id in the database" << endl;
        }
        else
        {
                worker[j].modEmp(i);
        }

        cout << "Would you like to modify another employee? (Y/N)" << endl;
        cin >> choice;
        }
        while (choice == 'Y' || choice == 'y');

        cout << "Employee(s) modified." << endl;
}

void deleteEmployees(EmployeeList worker[MAX_SIZE])
{
        Employee temp;
        char choice;
        int i = 0;
        int count;

        do
        {
        cout << "\nEnter the employee's data to delete in the following order:  ID, First Name, Last Name," << endl;
        cout << "Pay Rate, and Hours Worked (with spaces, without the commas)." << endl;
        cin >> temp;

        count = worker[i].numEmp();

        worker[i].delEmp(temp);

        cout << "Would you like to delete another employee?" << endl;
        cin >> choice;
        }
        while (choice == 'Y' || choice == 'y');

        cout << "All employee's data deleted successfully." << endl;
}

void displayEmployeeData(EmployeeList worker[MAX_SIZE])
{
        string fileName = "EmployeeDataBase.dat";
        int i = 0;
        int count;

        count = worker[i].numEmp();

        worker[count] = worker[i].employeeHolder(fileName);

        cout << setw(10) << "ID" << setw(20) << "First Name" << setw(20) << "Last Name";
        cout << setw(10) << "Pay Rate" << setw(10) << "Hours" << setw(10) << "Wage" << endl;
        cout << "--------------------------------------------------------------------------------" << endl;

        for (int i = 0; i < count; i++)
        {
                cout << worker[i];
        }

}

void sortAndDisplayEmployeeData(EmployeeList worker[MAX_SIZE])
{
        int choice;
        int count, j = 0;

        cout << "Would you like to sort by:" << endl;
        cout << "1. Id" << endl;
        cout << "2. Name" << endl;
        cin >> choice;

        if (choice == 1)
        {
                count = worker[j].numEmp();
                worker[count] = worker[j].sortId();

                cout << setw(10) << "ID" << setw(20) << "First Name" << setw(20) << "Last Name";
                cout << setw(10) << "Pay Rate" << setw(10) << "Hours" << setw(10) << "Wage" << endl;
                cout << "--------------------------------------------------------------------------------" << endl;

                for (int i = 0; i < count; i++)
                {
                        cout << worker[i];
                }
        }

        if (choice == 2)
        {
                count = worker[j].numEmp();
                worker[count] = worker[j].sortName();

                cout << setw(10) << "ID" << setw(20) << "First Name" << setw(20) << "Last Name";
                cout << setw(10) << "Pay Rate" << setw(10) << "Hours" << setw(10) << "Wage" << endl;
                cout << "--------------------------------------------------------------------------------" << endl;

                for (int i = 0; i < count; i++)
                {
                        cout << worker[i];
                }
        }
}

EmployeeList.h:

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
#ifndef EMPLOYEELIST_H
#define EMPLOYEELIST_H

#include <iostream>
#include "Employee.h"
using namespace std;

const int MAX_SIZE = 50;

class EmployeeList
{
        private:
                Employee workers[MAX_SIZE];
                int count;
        public:
                void EmployeeListInfo(Employee EmployeeInfo, int count)
                {
                        workers[count] = EmployeeInfo;
                        count++;
                };

                EmployeeList employeeHolder(string);

                void addEmp(Employee &);

                void delEmp(Employee &);

                int searchEmp(int);

                EmployeeList sortId();

                EmployeeList sortName();

                void modEmp(int);

                int numEmp();

                Employee retEmp(int);

                friend ostream& operator<< (ostream&, EmployeeList &);

                friend istream& operator>> (istream&, EmployeeList &);

                void writeEmp();
};

#endif 


Employee.h
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
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <iostream>
using namespace std;

class Employee
{
        private:
                int id;
                char *fName;
                char *lName;
                double wage;
                double rate;
                int hours;
        public:
                void EmployeeInfo(char*, char*, int, double, int);

                //Mutators
                void setId(int ident)
                {
                        id = ident;
                };

                void setFName(char *name1)
                {
                        *fName = *name1;
                };

                void setLName(char *name2)
                {
                        *lName = *name2;
                };

                void setWage(double r, int h)
                {
                        wage = h * r;
                };

                void setRate(double payRate)
                {
                        rate = payRate;

                };

                void setHours(int hoursWorked)
                {
                        hours = hoursWorked;
                };

                //Fetchers
                int getId()
                {
                        return id;
                };

                char* getFName()
                {
                        return fName;
                };

                char* getLName()
                {
                        return lName;
                };

                double getWage()
                {
                        wage = rate * hours;
                        return wage;
                };

                double getRate()
                {
                        return rate;
                };

                int getHours()
                {
                        return hours;
                };

                bool operator == (Employee&);

                bool operator != (Employee&);

                friend ostream &operator << (ostream &, Employee &);

                friend istream &operator >> (istream &, Employee &);

                friend class EmployeeList;
};

#endif 


Do you need employeelist.cpp and employee.cpp also?
And I am not too familiar with classes, the prof still hasn't lectured enough to my satisfaction on them, and this is my first time using a class. So if there is any error you find, could you please explain thoroughly, or direct me to someplace I can read up on you? Thank you much!! :)
Last edited on
Pages: 12