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

11 Dsa

The document is a C++ program that manages student records using a binary file. It allows users to create, display, and delete student records, which include roll number, name, division, and address. The program utilizes a struct to define the student data and a class to handle the operations on the 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)
14 views3 pages

11 Dsa

The document is a C++ program that manages student records using a binary file. It allows users to create, display, and delete student records, which include roll number, name, division, and address. The program utilizes a struct to define the student data and a class to handle the operations on the 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>
using namespace std;

// Define struct for student


typedef struct {
int rollno;
char name[20];
char div; // Division
char address[50]; // Address
} stud;

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;

} while (choice == 'y' || choice == 'Y');

fout.close();
}

void student::Display() {
ifstream fin;
fin.open("stud.dat", ios::in | ios::binary);

stud rec;

cout << "\nStudent Records:\n";


while (fin.read((char*)&rec, sizeof(rec))) {
cout << "Roll Number: " << rec.rollno << endl;
cout << "Name: " << rec.name << endl;
cout << "Division: " << rec.div << endl;
cout << "Address: " << rec.address << endl;
cout << "-----------------------------\n";
}

fin.close();
}

void student::Delete() {
ifstream fin;
ofstream fout;

fin.open("stud.dat", ios::in | ios::binary);


fout.open("temp.dat", ios::out | ios::binary);

stud rec;
int roll;
cout << "Enter roll number to delete: ";
cin >> roll;

bool found = false;

while (fin.read((char*)&rec, sizeof(rec))) {


if (rec.rollno != roll) {
fout.write((char*)&rec, sizeof(rec));
} else {
found = true;
}
}

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";
}

} while (choice != 4);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy