Click here to Skip to main content
14,971,029 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have defined a class named employee.I have empname and three variables to store date(day,month and year).I have created e objects of employee.the problem is to sort employee according to date and print it....?
I have posted code below.... i tried to solve but i am not able to get it..please help..!


class employee
	char name[20];
	int day,month,year;
			cout<<"Enter Name:";
			cout<<"Enter Day:";
			cout<<"Enter Month:";
			cout<<"Enter Year:";
		void display()
			cout<<"Employee Name:"<<name<<endl;
void main()
	int i,j,k;
	employee tmp;
	employee *e=new employee[5];
    //	e->display();

[Edit] code block added - OriginalGriff [/Edit]
Updated 8-Mar-14 21:46pm
How is going to "define", "create", an so on? This is not a question, just the abuse.
I looks like you just copied some problem which was assigned to you. Is that right?
AP900 9-Mar-14 1:57am
The above is my question from my practical list.
below is the code i have written myself..i wrote the above question so that u can understand it...and give suitable answer...
Richard MacCutchan 9-Mar-14 4:43am
Sorry but there is too much wrong with the above code to provide a simple answer. You should not be using calls to cout and cin inside the constructor of your employee class. That should be done in main, and a new object created for each set of details. You could also one of the STL containers rather than a simple array to hold your list, as some of them have a built in sort function.
Stefan_Lang 10-Mar-14 7:00am
I totally agree, specifically regarding that constructor code! *gasp* Maybe you should post this as a solution, too!
Richard MacCutchan 10-Mar-14 7:51am
I think some more lessons would be more appropriate, or maybe a better teacher. :)

I'm not quite sure where to start here...

It doesn't sort by date because you aren't asking it to.
Your sort algorithm isn't comparing dates, it's comparing employees: which doesn't really have a comparison.

You need to change the simple comparison you are using at the moment to actually compare the dates! Which means either storing them as date items - a very good idea - or comparing the year, then the month, then the day to find out the actual sort order.

What I would do is extract your sort code into a separate function: hand it an array of Employee objects, and it returns a sorted array. Within this function, I'd call a function within the Employee class itself to compare it's date with another employee. It could return -1 for before, zero for equal, and 1 for later than.
As this is your homework, I will not provide any code, but a couple of hints that might lead you into right direction.

In main you are trying to do what is called a "brute force" sort, comparing each element with all others. That is no good sorting strategy, but it is a starting point.

What goes wrong in your code is that you construct two nested loops with indices i and j, but you compare elements e[j] and e[j+1] instead if e[i] and e[j]. So that won't work.

The second thing that goes wrong is that you j-loop covers the wrong elements. It should run from i+1 to the end of your array. The idea should be to find the smallest of all remaining elements above index i and assign it to cell i. That guarantees that all elements from the start of the array to index i-1 will be in the correct order.

Once you have that all running you should google for a better sorting algorithm (there are many of them, as sorting is an entire research field in its own). So it should not be hard to pick a better one and implement it.

Besides that there are many small things in your code that need to be fixed, too many to be listed in a quick answer. But I am sure you will detect all those when you run your program several times in a debugger.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900