12 Dsa
12 Dsa
#include <fstream>
#include <stdio.h>
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;
}
file.close();
}
void searchForRecord() {
int id;
Employee x;
int found = 0;
fstream file;
file.open("EMPLOYEE.DAT", ios::binary | ios::in);
if (!file) {
cout << "ERROR IN OPENING FILE!" << endl;
return;
}
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;
}