Name:- Suyash Shyamsundar Waghule
Roll no:- S512051
Div :- S.E B
Subject:- DSA
Practical No. :- 10
Code:-
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class tel {
public:
int rollNo;
char name[20];
char div;
char address[50];
void accept() {
cout << "\n\tEnter Roll Number: ";
cin >> rollNo;
cout << "\n\tEnter the Name: ";
cin >> name;
cout << "\n\tEnter the Division: ";
cin >> div;
cout << "\n\tEnter the Address: ";
[Link](); // Clear the newline character from the input buffer
[Link](address, 50); // Use getline for address to allow spaces
}
void show() {
cout << "\n\t" << rollNo << "\t\t" << name << "\t\t" << div << "\t\t" << address;
}
int getRollNo() {
return rollNo;
}
};
int main() {
tel t1;
fstream f, g;
int ch;
do {
cout << "\n>>>>>>>>>>>>>>>>>>>>>>MENU<<<<<<<<<<<<<<<<<<<<";
cout << "\n1. Insert and overwrite";
cout << "\n2. Show";
cout << "\n3. Search & Edit (by Roll Number)";
cout << "\n4. Search & Edit (by Name)";
cout << "\n5. Delete a Student Record";
cout << "\n6. Exit";
cout << "\n\tEnter your choice: ";
cin >> ch;
switch (ch) {
case 1: {
[Link]("[Link]", ios::out | ios::trunc);
do {
[Link]();
[Link]((char*)&t1, sizeof(t1));
cout << "\nDo you want to enter more records? (1. Yes, 2. No): ";
cin >> ch;
} while (ch == 1);
[Link]();
break;
}
case 2: {
[Link]("[Link]", ios::in);
if (!f) {
cout << "Error opening file!" << endl;
break;
}
cout << "\n\tRoll No.\tName\t\tDivision\tAddress";
while ([Link]((char*)&t1, sizeof(t1))) {
[Link]();
}
[Link]();
break;
}
case 3: {
int rec;
cout << "\nEnter the roll number you want to find: ";
cin >> rec;
[Link]("[Link]", ios::in | ios::out);
bool found = false;
while ([Link]((char*)&t1, sizeof(t1))) {
if ([Link]() == rec) {
cout << "\nRecord found. Modify the details:";
[Link]();
[Link](-sizeof(t1), ios::cur);
[Link]((char*)&t1, sizeof(t1));
found = true;
break;
}
}
if (!found) {
cout << "\nRecord not found." << endl;
}
[Link]();
break;
}
case 4: {
char name[20];
cout << "\nEnter the name you want to find and edit: ";
cin >> name;
[Link]("[Link]", ios::in | ios::out);
bool found = false;
while ([Link]((char*)&t1, sizeof(t1))) {
if (strcmp([Link], name) == 0) {
cout << "\nRecord found. Modify the details:";
[Link]();
[Link](-sizeof(t1), ios::cur);
[Link]((char*)&t1, sizeof(t1 ));
found = true;
break;
}
}
if (!found) {
cout << "\nRecord not found." << endl;
}
[Link]();
break;
}
case 5: {
int roll;
cout << "Please Enter the Roll No. of Student Whose Info You Want to Delete: ";
cin >> roll;
fstream tempFile("[Link]", ios::out);
[Link]("[Link]", ios::in);
while ([Link]((char*)&t1, sizeof(t1))) {
if ([Link]() != roll) {
[Link]((char*)&t1, sizeof(t1));
}
}
[Link]();
[Link]();
remove("[Link]");
rename("[Link]", "[Link]");
cout << "The record with the roll no. " << roll << " has been deleted." << endl;
break;
}
case 6:
cout << "\n\tThank you." << endl;
break;
default:
cout << "\nInvalid choice. Please try again." << endl;
}
} while (ch != 6);
return 0;
}
Output :-