Professional Documents
Culture Documents
#Include Iostream 2
#Include Iostream 2
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <limits>
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;
}
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;
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;
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;
file.seekp(file.tellg() - static_cast<streampos>(sizeof(Employee)));
file.write(reinterpret_cast<char*>(&emp), sizeof(Employee));
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;
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;
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;
}
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;
}
}
}
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;
inFile.close();
void applyForLeave() {
fstream file(DATA_FILE, ios::in | ios::out | ios::binary);
if (!file) {
cerr << "Error opening file for reading and writing." << endl;
return;
}
Employee emp;
bool found = false;
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;
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;
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;
}
}