/ Files / Staff.cpp
Staff.cpp
 1  #include <iostream>
 2  #include <string>
 3  using namespace std;
 4  
 5  class Staff {
 6  private:
 7      int id;
 8      string name;
 9      float salary;
10  
11  public:
12      void acceptData() {
13          cout << "Enter Staff ID: ";
14          cin >> id;
15          cin.ignore();
16          cout << "Enter Name: ";
17          getline(cin, name);
18          cout << "Enter Salary: ";
19          cin >> salary;
20      }
21  
22      void displayData() const {
23          cout << "ID: " << id << ", Name: " << name << ", Salary: " << salary << endl;
24      }
25  
26      string getName() const {
27          return name;
28      }
29  };
30  
31  void sortByName(Staff staff[], int count) {
32      for (int i = 0; i < count - 1; i++) {
33          for (int j = i + 1; j < count; j++) {
34              if (staff[i].getName() > staff[j].getName()) {
35                  swap(staff[i], staff[j]);
36              }
37          }
38      }
39  }
40  
41  int main() {
42      int numStaff, choice;
43      cout << "Enter the number of staff members: ";
44      cin >> numStaff;
45      Staff *staffList = new Staff[numStaff];
46  
47      do {
48          cout << "\nMenu:\n";
49          cout << "1. Accept Staff Data\n";
50          cout << "2. Display Staff Data\n";
51          cout << "3. Sort by Name\n";
52          cout << "4. Exit\n";
53          cout << "Enter your choice: ";
54          cin >> choice;
55  
56          switch (choice) {
57              case 1:
58                  for (int i = 0; i < numStaff; i++) {
59                      cout << "\nEnter details for Staff " << i + 1 << ":\n";
60                      staffList[i].acceptData();
61                  }
62                  break;
63              case 2:
64                  cout << "\nStaff Details:\n";
65                  for (int i = 0; i < numStaff; i++) {
66                      staffList[i].displayData();
67                  }
68                  break;
69              case 3:
70                  sortByName(staffList, numStaff);
71                  cout << "\nStaff list sorted by name!\n";
72                  break;
73              case 4:
74                  cout << "Exiting program...\n";
75                  break;
76              default:
77                  cout << "Invalid choice! Try again.\n";
78          }
79      } while (choice != 4);
80  
81      delete[] staffList;
82      return 0;
83  }