show the most entered salary between 2 employees using class c++

Viewed 280

so I have to write this c++ program that gets 2 employees information (including name, personnel id, and salary) and after comparing them, it will show the employee's name with the most salary. instead of the easy way, I should use class to write this. here is my code:

#include <iostream>
#include <conio.h>
using namespace std;

class employee{
  char emp_name[20];
  int emp_id;
  float emp_sal;
 
 public: 
 
  void input()
  { 
       cout << "Enter Employee's Name: ";
       cin >> emp_name;
      
       cout << "\nEnter Personnel ID: ";
       cin >> emp_id;
       
       cout << "\nEnter Salary: ";
       cin >> emp_sal;
  }
  
  void most_sal(){
    int max = 0;
    if (emp_sal > max){
        cout << emp_name;
      }
    }
  
  };

int main (){
    employee e1,e2;

   e1.input();
   e2.input();
   e1.most_sal();
   e2.most_sal();
    
}

I know that the most_sal part in void most_sal() is not correct but I don't know what I should write instead.

2 Answers

This is your problem:

  void most_sal(){
    int max = 0;
    if (emp_sal > max){
        cout << emp_name;
    }
  }

max is a local variable set to zero. Each time you call it, it is reset to zero. So the compare (assuming the employee is going to be paid) is always true.

What you really need to do is compare e1.emp_sal against e2.emp_sal in a simple if-statement in your main().

if (e1.emp_sal >= e2.emp_sal) {
     cout << e1.emp_name << endl;
}
else {
     cout << e2.emp_name << endl;
}

Hmm. Probably need to also do one of:

  1. Add a getter
  2. Make your data public
  3. Write a comparison method

A getter provides access to your private data. Within your class:

public:
     int getEmpSal() const { return emp_sal; }
     const char * getName() const { return emp_name; }

And then the if-statements above have to use getEmpSal() instead of emp_sal:

if (e1.getEmpSal() >= e2.getEmpSal()) {
     cout << e1.getEmpName() << endl;
}
else {
     cout << e2.getEmpName() << endl;
}

You can maintain a list of employees in a static member. To get the employee with the max salary you can operate on the static container using static member function. Consider using a vector here. Create a constructor with three parameters every you call the constructor push that employee to the static container. This way you can keep track of all the employees. To get the max employee we can use std::max_element

// include all relevant header files.
class employee {
  std::string emp_name;
  int emp_id;
  float emp_sal;
  static std::vector<employee> DB;

 public:
  employee(std::string name, int id, float salary)
      : emp_name(name), emp_id(id), emp_sal(salary) {
    DB.push_back(*this);
  }

  float salary() { return emp_sal; }
  std::string name() { return emp_name; }
  static void get_max_sal() {
    // std::max_element in algorithm header file.
    auto m = std::max_element(
        DB.begin(), DB.end(),
        [](employee e1, employee e2) { return e1.salary() < e2.salary(); });
    std::cout << m->name();
  }
};

std::vector<employee> employee::DB{};

int main() {
  employee e1("user1", 1, 24.f);
  employee e2("user2", 2, 34.f);
  employee e3("user3", 3, 12.f);
  employee e4("user4", 4, 44.f);
  employee::get_max_sal();
  return 0;
}
Related