Employee record management in C
Let us create a employee record management in C. A employee record has name, id and salary. The operations are adding the details, search an employee, delete a record and display the employee records.
Program implementation:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
struct Employee {
int e_id;
char e_name[50];
float e_salary;
};
struct Employee emp[MAX];
int count = 0;
void addEmployee() {
printf("\nEnter the Employee id: ");
scanf("%d", &emp[count].e_id);
printf("Enter
the Employee Name: ");
scanf("
%[^\n]", emp[count].e_name);
printf("Enter
the salary: ");
scanf("%f", &emp[count].e_salary);
count++;
printf("Employee details are added successfully!\n");
}
void displayIt() {
printf("\n---
Employee Records ---\n");
for (int i = 0; i
< count; i++) {
printf("id: %d | Name: %s | Salary: %.2f\n",
emp[i].e_id,
emp[i].e_name,
emp[i].e_salary);
}
}
void searchEmployee() {
int id;
printf("\nEnter the id to search: ");
scanf("%d", &id);
for (int i = 0; i
< count; i++) {
if
(emp[i].e_id == id) {
printf("Employee is Found: %s with salary %.2f\n",
emp[i].e_name, emp[i].e_salary);
return;
}
}
printf("Employee details are not found.\n");
}
void deleteEmployee() {
int id;
printf("\nEnter employee id to delete: ");
scanf("%d", &id);
for (int i = 0; i
< count; i++) {
if
(emp[i].e_id == id) {
for (int j
= i; j < count - 1; j++) {
emp[j]
= emp[j + 1];
}
count--;
printf("Employee
details are deleted successfully.\n");
return;
}
}
printf("Employee details are not found.\n");
}
int main() {
int choice;
do {
printf("\n--- Employee Details ---\n");
printf("1. Add Employee\n");
printf("2. Display Employee Details\n");
printf("3. Search Employee\n");
printf("4. Delete Employee\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch
(choice) {
case 1:
addEmployee(); break;
case 2:
displayIt(); break;
case 3:
searchEmployee(); break;
case 4:
deleteEmployee(); break;
case 5:
printf("Exiting...\n"); break;
default:
printf("Invalid choice!\n");
}
} while (choice !=
5);
return 0;
}
Output:
--- Employee Details ---
1. Add Employee
2. Display Employee Details
3. Search Employee
4. Delete Employee
5. Exit
Enter your choice: 1
Enter the Employee id: 11
Enter the Employee Name: Ajay
Enter the salary: 23000
Employee details are added successfully!
--- Employee Details ---
1. Add Employee
2. Display Employee Details
3. Search Employee
4. Delete Employee
5. Exit
Enter your choice: 1
Enter the Employee id: 12
Enter the Employee Name:
Bob
Enter the salary: 45000
Employee details are added successfully!
1. Add Employee
2. Display Employee Details
3. Search Employee
4. Delete Employee
5. Exit
Enter your choice: 1
Enter the Employee id: 13
Enter the Employee Name: Jeyanth
Enter the salary: 67509
Employee details are added successfully!
--- Employee Details ---
1. Add Employee
2. Display Employee Details
3. Search Employee
4. Delete Employee
5. Exit
Enter your choice: 2
--- Employee Records ---
id: 11 | Name: Ajay | Salary: 23000.00
id: 12 | Name: Bob | Salary: 45000.00
id: 13 | Name: Jeyanth | Salary: 67509.00
--- Employee Details ---
1. Add Employee
2. Display Employee Details
3. Search Employee
4. Delete Employee
5. Exit
Enter your choice: 3
Enter the id to search: 12
Employee is Found: Bob with salary 45000.00
--- Employee Details ---
1. Add Employee
2. Display Employee Details
3. Search Employee
4. Delete Employee
5. Exit
Enter your choice: 4
Enter employee id to delete: 13
Employee details are deleted successfully.
--- Employee Details ---
1. Add Employee
2. Display Employee Details
3. Search Employee
4. Delete Employee
5. Exit
Enter your choice: 2
--- Employee Records ---
id: 11 | Name: Ajay | Salary: 23000.00
id: 12 | Name: Bob | Salary: 45000.00
--- Employee Details ---
1. Add Employee
2. Display Employee Details
3. Search Employee
4. Delete Employee
5. Exit
Enter your choice: 5
Exiting...
Hope, this code is useful to you. Keep coding!!!!!
Comments
Post a Comment