0% found this document useful (0 votes)
10 views3 pages

12 Dsa

The document is a C++ program that manages employee records, allowing users to add, display, and search for employee information stored in a binary file. It defines an Employee class with attributes for employee code, name, and salary, and provides methods for reading from input and displaying employee details. The main function presents a menu for user interaction to perform various operations on the employee records.

Uploaded by

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

12 Dsa

The document is a C++ program that manages employee records, allowing users to add, display, and search for employee information stored in a binary file. It defines an Employee class with attributes for employee code, name, and salary, and provides methods for reading from input and displaying employee details. The main function presents a menu for user interaction to perform various operations on the employee records.

Uploaded by

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

#include <iostream>

#include <fstream>
#include <stdio.h>

using namespace std;

class Employee {
private:
int code;
char name[20];
float salary;
public:
void read();
void display();
int getEmpCode() { return code; }
int getSalary() { return salary; }
};

void Employee::read() {
cout << "Enter employee code: ";
cin >> code;
cout << "Enter name: ";
cin.ignore();
cin.getline(name, 20);
cout << "Enter salary: ";
cin >> salary;
}

void Employee::display() {
cout << code << "\t" << name << "\t" << salary << endl;
}

void appendToFile() {
Employee x;
x.read();

fstream file;
file.open("EMPLOYEE.DAT", ios::binary | ios::app);
file.write((char*)&x, sizeof(x));
file.close();
cout << "Record added successfully!" << endl;
}

void displayAll() {
Employee x;
fstream file;
file.open("EMPLOYEE.DAT", ios::binary | ios::in);
if (!file) {
cout << "ERROR IN OPENING FILE!" << endl;
return;
}

while (file.read((char*)&x, sizeof(x))) {


if (x.getSalary() > 10000 && x.getSalary() <= 20000) {
x.display();
}
}

file.close();
}

void searchForRecord() {
int id;
Employee x;
int found = 0;

cout << "Enter employee code: ";


cin >> id;

fstream file;
file.open("EMPLOYEE.DAT", ios::binary | ios::in);
if (!file) {
cout << "ERROR IN OPENING FILE!" << endl;
return;
}

while (file.read((char*)&x, sizeof(x))) {


if (x.getEmpCode() == id) {
cout << "RECORD FOUND!" << endl;
x.display();
found = 1;
break;
}
}

if (!found) {
cout << "Record not found!" << endl;
}

file.close();
}

int main() {
int n;
char ch;

do {
cout << "ENTER CHOICE" << endl;
cout << "1: ADD AN EMPLOYEE" << endl;
cout << "2: DISPLAY" << endl;
cout << "3: SEARCH" << endl;
cout << "Make a choice: ";
cin >> n;

switch (n) {
case 1:
appendToFile();
break;
case 2:
displayAll();
break;
case 3:
searchForRecord();
break;
default:
cout << "Invalid Choice!" << endl;
}
cout << "Do you want to continue? (Y/N): ";
cin >> ch;
} while (ch == 'Y' || ch == 'y');

return 0;
}

You might also like