0% found this document useful (0 votes)
20 views8 pages

Prog 12

The document describes a C++ program for managing employee information, allowing users to add, delete, search, and display employee records using a binary file. It includes functions for each operation and a main menu for user interaction. The program handles employee details such as ID, name, designation, and salary, and provides appropriate messages for successful operations or errors.

Uploaded by

flipobeat12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Prog 12

The document describes a C++ program for managing employee information, allowing users to add, delete, search, and display employee records using a binary file. It includes functions for each operation and a main menu for user interaction. The program handles employee details such as ID, name, designation, and salary, and provides appropriate messages for successful operations or errors.

Uploaded by

flipobeat12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Name : Yash Sonawane

Practical No : 12(F)

Batch : S3

Roll No : 61

Branch : AI&DS

/* Company maintains employee information as employee ID, name, designation and salary. Allow
user to add, delete information of employee. Display information of particular employee. If
employee does not exist an appropriate message is displayed. If it is, then the system displays the
employee details. Use index sequential file to maintain the data. */

#include <iostream>

#include <fstream>

#include <string> using

namespace std;

struct Employee {

int emp_id; char

name[30]; char

designation[20];

float salary;

};

void addEmployee() { ofstream outFile("employees.dat",

ios::app | ios::binary);

Employee emp;

cout << "Enter Employee ID: ";

cin >> emp.emp_id;

cin.ignore(); cout <<

"Enter Name: ";

cin.getline(emp.name, 30);
cout << "Enter Designation:

";

cin.getline(emp.designation,

20); cout << "Enter Salary:

"; cin >> emp.salary;

outFile.write((char*)&emp, sizeof(emp));

outFile.close();

cout << "Employee added successfully!\n";

void displayEmployees() { ifstream

inFile("employees.dat", ios::binary);

Employee emp;

cout << "\nEmployee Details:\n"; cout <<

"ID\tName\t\tDesignation\tSalary\n";

cout << "-------------------------------------------------\n";

while (inFile.read((char*)&emp, sizeof(emp))) {

cout << emp.emp_id << "\t" << emp.name << "\t" << emp.designation << "\t" << emp.salary <<
"\n";

inFile.close();

void searchEmployee() { ifstream

inFile("employees.dat", ios::binary);

Employee emp;
int searchID;

bool found = false;

cout << "Enter Employee ID to search: ";

cin >> searchID;

while (inFile.read((char*)&emp, sizeof(emp))) {

if (emp.emp_id == searchID) { cout << "\

nEmployee Found:\n";

cout << "ID: " << emp.emp_id << "\nName: " << emp.name << "\nDesignation: " <<
emp.designation << "\nSalary: " << emp.salary << "\n";

found = true;

break;

if (!found) {

cout << "\nEmployee with ID " << searchID << " not found.\n";

inFile.close();

void deleteEmployee() { ifstream

inFile("employees.dat", ios::binary);

ofstream outFile("temp.dat", ios::binary);

Employee emp;

int deleteID;

bool found = false;


cout << "Enter Employee ID to delete: ";

cin >> deleteID;

while (inFile.read((char*)&emp, sizeof(emp))) { if (emp.emp_id ==

deleteID) { cout << "\nEmployee with ID " << deleteID << " deleted

successfully.\n"; found = true;

} else {

outFile.write((char*)&emp, sizeof(emp));

if (!found) { cout << "\nEmployee with ID " << deleteID << "

not found.\n";

inFile.close(); outFile.close();

remove("employees.dat");

rename("temp.dat", "employees.dat");

int main()

{ int choice;

do {

cout << "\nMenu:\n";

cout << "1. Add Employee\n2. Display Employees\n3. Search Employee\n4. Delete Employee\
n5. Exit\n"; cout << "Enter your choice: "; cin >> choice;

switch (choice) {
case 1: addEmployee(); break; case 2:

displayEmployees(); break; case 3:

searchEmployee(); break; case 4: deleteEmployee();

break; case 5: cout << "Exiting program...\n"; break;

default: cout << "Invalid choice! Please try again.\n";

} while (choice != 5);

return 0;

OUTPUT :
(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ cd ..

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:/home$ cd computer/

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ g++ Program12.cpp

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ ./a.out

Main Menu

1. Create

2. Display

3. Update

4. Delete

5. Append

6. Search

7. Exit

Enter your choice: 1

Enter Name: John

Enter Emp_ID: 101

Enter Salary: 50000

Do you want to add more records? (y/n): y

Enter Name: Alice

Enter Emp_ID: 102

Enter Salary: 55000

Do you want to add more records? (y/n): n

Main Menu
1. Create

2. Display

3. Update

4. Delete

5. Append

6. Search 7. Exit

Enter your choice: 2

The Contents of the file are:

Name: John

Emp_ID: 101

Salary: 50000

Name: Alice

Emp_ID: 102

Salary: 55000

Main Menu

1. Create

2. Display

3. Update

4. Delete

5. Append

6. Search 7. Exit

Enter your choice: 6


Main Menu

1. Create

2. Display

3. Update

4. Delete

5. Append

6. Search

7. Exit

Enter your choice: 7

Exiting the program...

You might also like