Untitled

 avatar
unknown
plain_text
a year ago
2.0 kB
17
Indexable
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

struct Employee 
{
    string name;
    int empid;
    string businessTitle;
    string doj;
};

typedef void(*DisplayCallback)(vector<Employee>&);
typedef void(*SortCallback)(vector<Employee>&);

void display(vector<Employee>& v)
{
    cout << "Displaying All employee details: " << endl;
    for(const Employee& e : v)
    {
        cout << e.name << endl;
        cout << e.empid << endl;
        cout << e.businessTitle << endl;
        cout << e.doj << endl;
        cout << "\n";
    }
    cout << "\n";
}

void sortEmp(vector<Employee>& v)
{
    std::sort(v.begin(), v.end(), [](const Employee& emp1, const Employee& emp2) {
        return emp1.empid < emp2.empid; 
    });
}

void addEmployee(vector<Employee>& v, DisplayCallback callbackPrint)
{
    Employee e;
    cout << "Enter the name of the employee: ";
    cin >> e.name;
    
    cout << "Enter the employee ID: ";
    cin >> e.empid;
    
    cout << "Enter the business title of the employee: ";
    cin >> e.businessTitle;

    cout << "Enter the date of joining of the employee: ";
    cin >> e.doj;
    
    v.push_back(e);
    fflush(stdin);
    
    callbackPrint(v);
}

int main()
{
    cout << "Enter 1 to add employee details: \n";
    cout << "Enter 2 to sort employee details: \n";
    cout << "Enter 3 to display: \n";
    cout << "Enter 4 to exit: \n";
    vector<Employee> v; 
    
    int n;
    while(true)
    {
        cout << "Choose number: ";
        cin >> n;
        
        switch(n)
        {
            case 1:
                addEmployee(v, display);
                break;
            case 2:
                sortEmp(v);
                break;
            case 3:
                display(v);
                break;
            case 4:
                return 0;
            default:
                return 0;
        }
    }
}
Editor is loading...
Leave a Comment