#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <limits>
using namespace std;
const char DATA_FILE[] = "employee_data.txt";
struct Employee {
int id;
char name[50];
double salary;
int leaveBalance;
double rating;
int daysPresent; // New field for employee attendance
};
// Function prototypes
void displayMenu();
void addEmployee();
void viewAllEmployees();
void searchEmployeeByID();
void updateEmployeeSalary();
void deleteEmployee();
void calculateAverageSalary();
void sortEmployeesBySalary();
void displayHighestLowestSalary();
void applyForLeave();
void rateEmployee();
void markEmployeeAttendance();
int main() {
int choice;
do {
displayMenu();
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear input buffer
switch (choice) {
case 1:
addEmployee();
break;
case 2:
viewAllEmployees();
break;
case 3:
searchEmployeeByID();
break;
case 4:
updateEmployeeSalary();
break;
case 5:
deleteEmployee();
break;
case 6:
calculateAverageSalary();
break;
case 7:
sortEmployeesBySalary();
break;
case 8:
displayHighestLowestSalary();
break;
case 9:
applyForLeave();
break;
case 10:
rateEmployee();
break;
case 11:
markEmployeeAttendance();
break;
case 12:
cout << "Exiting program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 12);
return 0;
}
// Rest of the code remains unchanged
void displayMenu() {
cout << "1. Add Employee" << endl;
cout << "2. View All Employees" << endl;
cout << "3. Search Employee by ID" << endl;
cout << "4. Update Employee Salary" << endl;
cout << "5. Delete Employee" << endl;
cout << "6. Calculate Average Salary" << endl;
cout << "7. Sort Employees by Salary" << endl;
cout << "8. Display Highest and Lowest Salary" << endl;
cout << "9. Apply for Leave" << endl;
cout << "10. Rate Employee" << endl;
cout << "11. Mark Employee Attendance" << endl;
cout << "12. Exit" << endl;
cout << "Enter your choice: ";
}
void addEmployee() {
// Your existing code for addEmployee function
}
void viewAllEmployees() {
ifstream inFile(DATA_FILE, ios::binary);
if (!inFile) {
cerr << "Error opening file for reading." << endl;
return;
}
Employee emp;
cout << left << setw(10) << "ID" << setw(20) << "Name" << setw(10) << "Salary"
<< setw(15) << "Leave Balance"
<< setw(15) << "Rating" << setw(20) << "Days Present" << endl;
cout << setfill('-') << setw(80) << "" << setfill(' ') << endl;
while (inFile.read(reinterpret_cast<char*>(&emp), sizeof(Employee))) {
cout << left << setw(10) << "ID: " << emp.id << endl
<< setw(20) << "Name: " << emp.name << endl
<< setw(10) << "Salary: " << emp.salary << endl
<< setw(15) << "Leave Balance: " << emp.leaveBalance << endl
<< setw(15) << "Rating: " << emp.rating << endl
<< setw(20) << "Days Present: " << emp.daysPresent << endl;
cout << setfill('-') << setw(80) << "" << setfill(' ') << endl;
}
inFile.close(); // Close the file at the end of the function
}
void searchEmployeeByID() {
ifstream inFile(DATA_FILE, ios::binary);
if (!inFile) {
cerr << "Error opening file for reading." << endl;
return;
}
int searchID;
cout << "Enter employee ID to search: ";
cin >> searchID;
Employee emp;
bool found = false;
while (inFile.read(reinterpret_cast<char*>(&emp), sizeof(Employee))) {
if (emp.id == searchID) {
cout << "Employee found:" << endl;
cout << "ID: " << emp.id << ", Name: " << emp.name << ", Salary: " <<
emp.salary
<< ", Leave Balance: " << emp.leaveBalance << ", Rating: " <<
emp.rating
<< ", Days Present: " << emp.daysPresent << endl;
found = true;
break;
}
}
if (!found) {
cout << "Employee not found." << endl;
}
inFile.close();
}
void updateEmployeeSalary() {
fstream file(DATA_FILE, ios::in | ios::out | ios::binary);
if (!file) {
cerr << "Error opening file for reading and writing." << endl;
return;
}
int updateID;
cout << "Enter employee ID to update salary: ";
cin >> updateID;
Employee emp;
bool found = false;
while (file.read(reinterpret_cast<char*>(&emp), sizeof(Employee))) {
if (emp.id == updateID) {
cout << "Enter new salary: ";
cin >> emp.salary;
file.seekp(file.tellg() - static_cast<streampos>(sizeof(Employee)));
file.write(reinterpret_cast<char*>(&emp), sizeof(Employee));
cout << "Salary updated successfully." << endl;
found = true;
break;
}
}
if (!found) {
cout << "Employee not found." << endl;
}
file.close();
}
void deleteEmployee() {
ifstream inFile(DATA_FILE, ios::binary);
ofstream outFile("temp.txt", ios::binary); // Temporary file
if (!inFile || !outFile) {
cerr << "Error opening files for reading/writing." << endl;
return;
}
int deleteID;
cout << "Enter employee ID to delete: ";
cin >> deleteID;
Employee emp;
bool found = false;
while (inFile.read(reinterpret_cast<char*>(&emp), sizeof(Employee))) {
if (emp.id == deleteID) {
cout << "Employee deleted successfully." << endl;
found = true;
} else {
outFile.write(reinterpret_cast<char*>(&emp), sizeof(Employee));
}
}
inFile.close();
outFile.close();
if (!found) {
cout << "Employee not found." << endl;
remove("temp.txt"); // Remove the temporary file if the employee is not
found
} else {
remove(DATA_FILE); // Remove the original file
rename("temp.txt", DATA_FILE); // Rename the temporary file
}
}
void calculateAverageSalary() {
ifstream inFile(DATA_FILE, ios::binary);
if (!inFile) {
cerr << "Error opening file for reading." << endl;
return;
}
Employee emp;
double totalSalary = 0.0;
int employeeCount = 0;
while (inFile.read(reinterpret_cast<char*>(&emp), sizeof(Employee))) {
totalSalary += emp.salary;
employeeCount++;
}
inFile.close();
if (employeeCount > 0) {
double averageSalary = totalSalary / employeeCount;
cout << "Average Salary: " << averageSalary << endl;
} else {
cout << "No employees found." << endl;
}
}
void sortEmployeesBySalary() {
ifstream inFile(DATA_FILE, ios::binary);
if (!inFile) {
cerr << "Error opening file for reading." << endl;
return;
}
Employee empArray[100]; // Assuming a maximum of 100 employees
int employeeCount = 0;
while (inFile.read(reinterpret_cast<char*>(&empArray[employeeCount]),
sizeof(Employee))) {
employeeCount++;
}
inFile.close();
if (employeeCount > 0) {
// Perform a simple bubble sort based on salary
for (int i = 0; i < employeeCount - 1; i++) {
for (int j = 0; j < employeeCount - i - 1; j++) {
if (empArray[j].salary > empArray[j + 1].salary) {
// Swap the elements
Employee temp = empArray[j];
empArray[j] = empArray[j + 1];
empArray[j + 1] = temp;
}
}
}
// Display the sorted list
cout << left << setw(10) << "ID" << setw(20) << "Name" << setw(10) <<
"Salary" << setw(15) << "Leave Balance"
<< setw(15) << "Rating" << setw(20) << "Days Present" << endl;
cout << setfill('-') << setw(80) << "" << setfill(' ') << endl;
for (int i = 0; i < employeeCount; i++) {
cout << setw(10) << empArray[i].id << setw(20) << empArray[i].name <<
setw(10) << empArray[i].salary
<< setw(15) << empArray[i].leaveBalance << setw(15) <<
empArray[i].rating
<< setw(20) << empArray[i].daysPresent << endl;
}
} else {
cout << "No employees found." << endl;
}
}
void displayHighestLowestSalary() {
ifstream inFile(DATA_FILE, ios::binary);
if (!inFile) {
cerr << "Error opening file for reading." << endl;
return;
}
Employee emp;
double highestSalary = 0.0, lowestSalary = numeric_limits<double>::max();
int highestID, lowestID;
while (inFile.read(reinterpret_cast<char*>(&emp), sizeof(Employee))) {
if (emp.salary > highestSalary) {
highestSalary = emp.salary;
highestID = emp.id;
}
if (emp.salary < lowestSalary) {
lowestSalary = emp.salary;
lowestID = emp.id;
}
}
inFile.close();
cout << "Employee with the highest salary:" << endl;
cout << "ID: " << highestID << ", Salary: " << highestSalary << endl;
cout << "Employee with the lowest salary:" << endl;
cout << "ID: " << lowestID << ", Salary: " << lowestSalary << endl;
}
void applyForLeave() {
fstream file(DATA_FILE, ios::in | ios::out | ios::binary);
if (!file) {
cerr << "Error opening file for reading and writing." << endl;
return;
}
int empID, leaveDays;
cout << "Enter employee ID to apply for leave: ";
cin >> empID;
Employee emp;
bool found = false;
while (file.read(reinterpret_cast<char*>(&emp), sizeof(Employee))) {
if (emp.id == empID) {
cout << "Enter the number of leave days: ";
cin >> leaveDays;
if (leaveDays > 0) {
emp.leaveBalance += leaveDays;
cout << "Leave applied successfully. Updated leave balance: " <<
emp.leaveBalance << " days." << endl;
file.seekp(file.tellg() -
static_cast<streampos>(sizeof(Employee)));
file.write(reinterpret_cast<char*>(&emp), sizeof(Employee));
} else {
cout << "Invalid number of leave days. Leave balance remains
unchanged." << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Employee not found." << endl;
}
file.close();
}
void rateEmployee() {
fstream file(DATA_FILE, ios::in | ios::out | ios::binary);
if (!file) {
cerr << "Error opening file for reading and writing." << endl;
return;
}
int empID;
double employeeRating;
cout << "Enter employee ID to rate: ";
cin >> empID;
Employee emp;
bool found = false;
while (file.read(reinterpret_cast<char*>(&emp), sizeof(Employee))) {
if (emp.id == empID) {
cout << "Enter the rating for the employee (0.0 to 5.0): ";
cin >> employeeRating;
if (employeeRating >= 0.0 && employeeRating <= 5.0) {
emp.rating = employeeRating;
cout << "Employee rated successfully. Updated rating: " <<
emp.rating << endl;
file.seekp(file.tellg() -
static_cast<streampos>(sizeof(Employee)));
file.write(reinterpret_cast<char*>(&emp), sizeof(Employee));
} else {
cout << "Invalid rating. Rating should be between 0.0 and 5.0." <<
endl;
}
found = true;
break;
}
}
if (!found) {
cout << "Employee not found." << endl;
}
file.close();
}
void markEmployeeAttendance() {
fstream file(DATA_FILE, ios::in | ios::out | ios::binary);
if (!file) {
cerr << "Error opening file for reading and writing." << endl;
return;
}
int empID;
cout << "Enter employee ID to mark attendance: ";
cin >> empID;
Employee emp;
bool found = false;
while (file.read(reinterpret_cast<char*>(&emp), sizeof(Employee))) {
if (emp.id == empID) {
// Increment the daysPresent field to mark attendance
emp.daysPresent++;
cout << "Attendance marked successfully. Updated days present: " <<
emp.daysPresent << endl;
file.seekp(file.tellg() - static_cast<streampos>(sizeof(Employee)));
file.write(reinterpret_cast<char*>(&emp), sizeof(Employee));
found = true;
break;
}
}
file.close();
if (!found) {
cout << "Employee not found." << endl;
}
}