I am trying to create a program that stores the data of employees and then displays the highest salary employee separately.
Here, What I had tried.
#include <iostream>
#include <limits>
#include <vector>
// Using this statement in order to reduce the spam of 'std::' here
using namespace std;
class Employee {
int ID;
string name;
string post;
long int salary;
public:
Employee() {}
// Initializing the class object
void initEmployee(int id, string varName, string varPost, long sal) {
ID = id, \
name = varName, \
post = varPost, \
salary = sal;
}
// 'salary' getter
long getSalary() {
return salary;
}
// The friend function
friend void maxSalary(vector<Employee> data) {
int max = 0;
for (size_t i{1}, len = data.size(); i < len; i )
// Comparison
if (data[i].getSalary() > data[i - 1].getSalary())
max = data[i].getSalary();
// After the loop execution, prints the maximum salary
std::cout << "Maximum salary: " << max << endl;
}
};
int main(void) {
int total;
cout << "Enter the total number of employees: ";
cin >> total;
vector<Employee> emp(total);
for (int i{}; i < total; i ) {
// Temporary variables to store data in each iteration
Employee temp;
string tempName;
string tempPost;
int tempID;
long int tempSal;
cout << "Name of the employee " << i 1 << ": ";
getline(cin, tempName);
// Clearing the 'cin' in order to prevent the getline skips
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Post of the employee: ";
getline(cin, tempPost);
cout << "Employee ID: ";
cin >> tempID;
cout << "Total salary: ";
cin >> tempSal;
// Initializing the temporary object
temp.initEmployee(tempID, tempName, tempPost, tempSal);
// Pushing the object into the main vector
emp.push_back(temp);
}
// Comparing the vector elements (note: it is a friend function)
maxSalary(emp);
return 0;
}
OUTPUT:
Enter the total number of employees: 2 // Number of employees
Name of the employee 1: John Doe // Employee 1
Post of the employee: Manager
Employee ID: 100
Total salary: 15000
Name of the employee 2: Max Ville // Employee 2
Post of the employee: Assitant Manager
Employee ID: 102
Total salary: 50000
Maximum salary: 50000 // Maximum salary
I can find the max salary of that employees... But I want to also display the details of that employee along with the salary. Anyone could help me in re-writing this program? If you have some other way to do this you are welcome
REQUIRED OUTPUT:
Enter the total number of employees: 2 // Number of employees
Name of the employee 1: John Doe // Employee 1
Post of the employee: Manager
Employee ID: 100
Total salary: 15000
Name of the employee 2: Max Ville // Employee 2
Post of the employee: Assitant Manager
Employee ID: 102
Total salary: 50000
Max. salary Employee:
Name Post ID Salary
max ville Assistant Manager 102 50000
CodePudding user response:
This
vector<Employee> emp(total);
doesnt do what you think it does. Just do
vector<Employee> emp;
if you want to reserve space for 'total' elements do
emp.reserve(total);
CodePudding user response:
You should return the index of the employee from the maxSalary(...)
function. Then you could use that index to print the details of that employee.
You should compare the salary of the current employee with the maximum salary so far. But instead, you compared it with the previous salary, which is wrong.
int maxSalary(vector<Employee> data) {
int max = 0;
int indexOfMax = 0;
for (size_t i = 0, len = data.size(); i < len; i )
if (data[i].getSalary() > max) {
max = data[i].getSalary();
indexOfMax = i;
}
}
return indexOfMax;
}
Then you can use this function like following lines:
int indexOfEmployeWithMaxSalary = maxSalary(emp);
Employee employee = emp[indexOfEmployeeWithMaxSalary];
std::cout << employee.name << " " << employee.post << ...
Also, it looks like there are some other bugs in your code.
First, you set size of your vector as total
. Then you try to push_back()
new elements to it. This will push your element to the total 1
position of your vector.
If you want to have an initial-sized vector, add your elements via subscripts, like emp[i] = temp;
. Otherwise don't set an initial size to your vector so you can use push_back()
.