C++ sorting class objects

Posted on 2004-10-07
Last Modified: 2010-04-01
I am new to C++ and OOP. I have a program that prompts an end user for user data, calculate pay, and display the information as an employee record. I am attempting to sort the employee records by employee numbers. I have worked out the logic for the sort (below) but I am having problems displaying the data with the corresponding employee numbers.

Here is my logic for the sort:
Employee e[5];
int i,j,Employee temp
for (i=0; i<5; i++) n[i]=rand()%i101
for (i=0; i<4; i++)
      for(j=i+1; j<5; j++)
      if (e[i].getEmpNo() > e[j].getEmpNo())
      {temp = e[i]; e[i]= e[j]; e[j]=temp;}

Here is my working program:
// H81.cpp : This program prompts the end user five times for specific employee data, calculate the pay, and displays the employee record
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Employee
                  int empNo;
                  string name;
                  char gender;
                  int age;
                  double rate, hours, pay, tax, netPay;

                  void setEmployee(int e, string n, char g, int a, double r, double h)
                        {      empNo=e; name=n; gender=g; age=a; rate=r; hours=h;            }

                  void getEmployee() //User is prompted for employee info
                              cout << "+----+----+ ENTER EMPLOYEE DATA +----+----+" << endl;
                              cout << "Employee Number: "; cin >> empNo;
                              cout << "Employee Name: "; cin >> name;
                              cout << "Gender: "; cin >> gender;
                              cout << "Age: "; cin >> age;
                              cout << "Hourly Rate: "; cin >> rate;
                              cout << "Hours Worked: "; cin >> hours;
                              cout << " " << endl;

                  void calculatePay(double r, double h) // Pay calculated here
                        {rate=r; hours=h; pay=r*h; tax=.31*pay; netPay=pay-tax;}

                  int getEmpNo() {return empNo;}

                  void showEmployee() // Employee record displayed here
                        {   cout << "+----+----+  Employee Record +---+----+ " << endl;
                              cout << "Employee Number: " << empNo << endl;
                              cout << "Employee Name: " << name << endl;
                              cout << "Gender: " << gender << endl;
                              cout << "Age: " << age << endl;
                              cout << "Hourly Rate: " << rate << endl;
                              cout << "Hours Worked: " << hours << endl;
                              cout << "Gross Pay: " << pay << endl;
                              cout << "Taxes (31%): " << tax << endl;
                              cout << "Net Pay: " << netPay << endl;                              

int main(int argc, char* argv[])
      Employee e[5];
      int x;

      e[0].setEmployee(55555, "Mary", 'F', 21, 15.50, 40.00); // Initializing arg
      for (x=0; x<5; x++)
                  e[x].calculatePay(15.00, 40.00);
      for (x=0; x<5; x++)
    return 0;
Question by:mjaugust
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 30

Expert Comment

ID: 12256034
>>I am having problems displaying the data with the corresponding employee numbers.

Please explain in detail the exact problem you're having, and post a specific question.

Your above post does not have an actual question.

Accepted Solution

teratoma earned 200 total points
ID: 12256237
One way C++ simplifies this type of thing is to provide standard containers and algorithms.  You could put your Employees in a std::vector or a std::list to start with.  Then, given that std::sort can sort any container (see note) given that the contained class defines operator< (less than), you only need to fill in the blanks:

class Employee
  bool operator<(const Employee& e)
    return ???;

Note: your class also needs a copy constructor (maybe an assignment operator too) if you want to use std::sort.  You don't need one if you store your objects in a list and use list::sort.

Expert Comment

ID: 12260570
 y not you use STL? for sorting object, you can just write a sorting algorithm only. All others will be done automatically. Look at STL algorithms and Templates.

All the best.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question