11 Dsa
11 Dsa
#include <fstream>
using namespace std;
class student {
public:
void Create();
void Display();
void Delete();
};
void student::Create() {
ofstream fout;
fout.open("stud.dat", ios::out | ios::binary);
stud rec;
char choice;
do {
cout << "Enter Roll Number: ";
cin >> rec.rollno;
cout << "Enter Name: ";
cin >> rec.name;
cout << "Enter Division (A/B/C): ";
cin >> rec.div;
cout << "Enter Address: ";
cin.ignore(); // clear input buffer
cin.getline(rec.address, 50);
fout.write((char*)&rec, sizeof(rec));
cout << "Do you want to add another record? (y/n): ";
cin >> choice;
fout.close();
}
void student::Display() {
ifstream fin;
fin.open("stud.dat", ios::in | ios::binary);
stud rec;
fin.close();
}
void student::Delete() {
ifstream fin;
ofstream fout;
stud rec;
int roll;
cout << "Enter roll number to delete: ";
cin >> roll;
fin.close();
fout.close();
remove("stud.dat");
rename("temp.dat", "stud.dat");
if (found) {
cout << "Record deleted successfully!\n";
} else {
cout << "Record not found!\n";
}
}
int main() {
student s;
int choice;
do {
cout << "\n1. Create Student Records";
cout << "\n2. Display Student Records";
cout << "\n3. Delete Student Record";
cout << "\n4. Exit";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1: s.Create(); break;
case 2: s.Display(); break;
case 3: s.Delete(); break;
case 4: cout << "Exiting...\n"; break;
default: cout << "Invalid choice!\n";
}
return 0;
}