PROGRAMMING FUINDAMENTALS
PROJECT-1
Group Members:
Sadia Iqbal
Aliya Iqbal
Ayesha Safdar
Topic: Employee Payroll System
Objective:
The objective of this program is to manage employee payroll
information using structures and arrays. It allows the user to
store, display, search, sort, calculate salary, add new employees,
and save employee records using a menu-driven approach.
Features:
• Uses structure to store employee data (ID, name, salary)
• Uses array of structures to manage multiple employees
• Allows initial entry of employee records
• Provides option to add new employees later
• Displays employee data in tabular form
• Calculates net salary with bonus
• Searching for employees by name
• Sorts employees by salary
• Prevents array overflow
• Saves employee data to a file
• Menu-driven and user-friendly
Program:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct Employee {
string name;
int id;
float basicSalary;
};
Employee emp[5];
int empCount = 0;
void inputData();
void displayData();
void calculateSalary();
void searchEmployee();
void sortEmployees();
void addEmployee();
void saveToFile();
int main() {
int choice;
inputData(); // initial employee input
do {
cout << "\n====== EMPLOYEE PAYROLL SYSTEM
======\n";
cout << "1. Display Employees\n";
cout << "2. Calculate Salary\n";
cout << "3. Search Employee\n";
cout << "4. Sort Employees\n";
cout << "5. Add New Employee\n";
cout << "6. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1: displayData(); break;
case 2: calculateSalary(); break;
case 3: searchEmployee(); break;
case 4: sortEmployees(); break;
case 5: addEmployee(); break;
case 6:
saveToFile();
cout << "Data saved. Exiting...\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 6);
return 0;
}
void inputData() {
cout << "How many employees? (max 5): ";
cin >> empCount;
if (empCount > 5) {
cout << "Maximum limit is 5 employees only.\n";
cout << "Only first 5 employees will be recorded.\n";
empCount = 5;
}
for (int i = 0; i < empCount; i++) {
cout << "\nEnter details of employee " << i + 1 << endl;
cout << "ID: ";
cin >> emp[i].id;
cout << "Name: ";
cin >> emp[i].name;
cout << "Basic Salary: ";
cin >> emp[i].basicSalary;
}
}
void displayData() {
cout << left << setw(10) << "ID"
<< setw(15) << "Name"
<< "Salary\n";
for (int i = 0; i < empCount; i++) {
cout << setw(10) << emp[i].id
<< setw(15) << emp[i].name
<< emp[i].basicSalary << endl;
}
}
void calculateSalary() {
int id;
cout << "Enter employee ID: ";
cin >> id;
for (int i = 0; i < empCount; i++) {
if (emp[i].id == id) {
float bonus = (emp[i].basicSalary > 35000) ? 5000 :
3000;
cout << "Net Salary = "
<< emp[i].basicSalary + bonus << endl;
return;
}
}
cout << "Employee not found!\n";
}
void searchEmployee() {
string key;
cout << "Enter employee name: ";
cin >> key;
for (int i = 0; i < empCount; i++) {
if (emp[i].name == key) {
cout << "Employee found! ID: "
<< emp[i].id << endl;
return;
}
}
cout << "Employee not found!\n";
}
void sortEmployees() {
for (int i = 0; i < empCount - 1; i++) {
for (int j = i + 1; j < empCount; j++) {
if (emp[i].basicSalary > emp[j].basicSalary) {
Employee temp = emp[i];
emp[i] = emp[j];
emp[j] = temp;
}
}
}
cout << "Employees sorted by salary!\n";
}
void addEmployee() {
if (empCount >= 5) {
cout << "Employee list is full!\n";
return;
}
cout << "\nEnter new employee details:\n";
cout << "ID: ";
cin >> emp[empCount].id;
cout << "Name: ";
cin >> emp[empCount].name;
cout << "Basic Salary: ";
cin >> emp[empCount].basicSalary;
empCount++;
cout << "Employee added successfully!\n";
}
void saveToFile() {
ofstream fout("[Link]");
for (int i = 0; i < empCount; i++) {
fout << emp[i].id << " "
<< emp[i].name << " "
<< emp[i].basicSalary << endl;
}
[Link]();
}