-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
144 lines (127 loc) · 3.59 KB
/
main.cpp
File metadata and controls
144 lines (127 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student {
private:
int rollNo;
string name;
string department;
public:
void input() {
cout << "Enter Roll No: ";
cin >> rollNo;
cin.ignore();
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter Department: ";
getline(cin, department);
}
void display() const {
cout << "Roll No: " << rollNo << "\n";
cout << "Name: " << name << "\n";
cout << "Department: " << department << "\n";
cout << "------------------------\n";
}
int getRollNo() const {
return rollNo;
}
};
void addStudent() {
ofstream file("students.dat", ios::binary | ios::app);
Student s;
s.input();
file.write(reinterpret_cast<char*>(&s), sizeof(s));
file.close();
cout << "Student record added successfully.\n";
}
void displayAll() {
ifstream file("students.dat", ios::binary);
Student s;
while (file.read(reinterpret_cast<char*>(&s), sizeof(s))) {
s.display();
}
file.close();
}
void searchStudent(int roll) {
ifstream file("students.dat", ios::binary);
Student s;
bool found = false;
while (file.read(reinterpret_cast<char*>(&s), sizeof(s))) {
if (s.getRollNo() == roll) {
cout << "Student Found:\n";
s.display();
found = true;
break;
}
}
file.close();
if (!found) {
cout << "Student with Roll No " << roll << " not found.\n";
}
}
void deleteStudent(int roll) {
ifstream inFile("students.dat", ios::binary);
ofstream outFile("temp.dat", ios::binary);
Student s;
bool found = false;
while (inFile.read(reinterpret_cast<char*>(&s), sizeof(s))) {
if (s.getRollNo() != roll) {
outFile.write(reinterpret_cast<char*>(&s), sizeof(s));
} else {
found = true;
}
}
inFile.close();
outFile.close();
remove("students.dat");
rename("temp.dat", "students.dat");
if (found)
cout << "Student record deleted successfully.\n";
else
cout << "Student with Roll No " << roll << " not found.\n";
}
int main() {
int choice, roll;
do {
cout << "\n===== Student Record Management System =====\n";
cout << "1. Add Student\n";
cout << "2. Display All Students\n";
cout << "3. Search Student by Roll No\n";
cout << "4. Delete Student by Roll No\n";
cout << "5. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
displayAll();
break;
case 3:
cout << "Enter Roll No to search: ";
cin >> roll;
searchStudent(roll);
break;
case 4:
cout << "Enter Roll No to delete: ";
cin >> roll;
deleteStudent(roll);
break;
case 5:
cout << "Exiting program...\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 5);
return 0;
}