[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 546
  • Last Modified:

C++ sorting class objects

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;
1 Solution
>>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.
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.
 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now