-
Notifications
You must be signed in to change notification settings - Fork 0
/
GA11.cpp
278 lines (236 loc) · 7.34 KB
/
GA11.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*Department maintains a student information. The filecontains roll
number, name, division, and address. Allowuser to add, delete information of
student. Display information of particular employee. If record of student doesnot
exist an appropriate message is displayed. If it is, then the
system displays the student details. Use sequential file to mainthe data.*/
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
struct Student
{
int roll_number;
string name;
char division;
string address;
};
void add_students()
{
Student s;
cout << "Enter the roll number of the student to be added-\n";
cin >> s.roll_number;
cout << "Enter the name of the student to be added-\n";
cin >> s.name;
cout << "Enter the division of the student to be added-\n";
cin >> s.division;
cout << "Enter the address of the student to be added-\n";
cin >> s.address;
ofstream write;
write.open("students.txt", ios::app);
write << "\n"
<< s.roll_number;
write << "\n"
<< s.name;
write << "\n"
<< s.division;
write << "\n"
<< s.address;
write.close();
}
void display(Student s)
{
cout << "Roll Number- " << s.roll_number << endl;
cout << "Name- " << s.name << endl;
cout << "Division- " << s.division << endl;
cout << "Address- " << s.address << endl;
cout << endl;
}
void read_students()
{
Student s;
ifstream read;
read.open("students.txt");
while (!read.eof())
{
read >> s.roll_number;
read >> s.name;
read >> s.division;
read >> s.address;
display(s);
}
read.close();
}
int search_students(int roll_number)
{
Student s;
ifstream read;
read.open("students.txt");
while (!read.eof())
{
read >> s.roll_number;
read >> s.name;
read >> s.division;
read >> s.address;
if (s.roll_number == roll_number)
{
cout << "Student's record found!" << endl;
return s.roll_number;
}
}
read.close();
cout << "Student's record not found!" << endl;
return -1;
}
void delete_students(int roll_number)
{
roll_number = search_students(roll_number);
if (roll_number == -1)
{
cout << "Student record to be deleted is not present in the file!" << endl;
return;
}
else
{
Student s;
ofstream write;
write.open("temp.txt", ios::app);
ifstream read;
read.open("students.txt");
while (!read.eof())
{
read >> s.roll_number;
read >> s.name;
read >> s.division;
read >> s.address;
if (s.roll_number != roll_number)
{
write << "\n"
<< s.roll_number;
write << "\n"
<< s.name;
write << "\n"
<< s.division;
write << "\n"
<< s.address;
}
}
write.close();
read.close();
remove("students.txt");
rename("temp.txt", "students.txt");
cout << "Student record to be deleted is deleted successfully!" << endl;
}
}
int main()
{
int choice;
do
{
cout << "-----STUDENT RECORDS-----" << endl;
cout << "Enter 1 for adding a record" << endl;
cout << "Enter 2 for displaying the records" << endl;
cout << "Enter 3 for searching a record" << endl;
cout << "Enter 4 for deleting a record" << endl;
cout << "Enter 0 to exit" << endl;
cin >> choice;
switch (choice)
{
case 1:
add_students();
break;
case 2:
read_students();
break;
case 3:
int roll_number;
cout << "Enter roll number of the student to be searched-\n";
cin >> roll_number;
search_students(roll_number);
break;
case 4:
cout << "Enter roll number of the student to be deleted-\n";
cin >> roll_number;
delete_students(roll_number);
break;
case 0:
break;
}
} while (choice != 0);
}
/*
-----STUDENT RECORDS-----
Enter 1 for adding a record
Enter 2 for displaying the records
Enter 3 for searching a record
Enter 4 for deleting a record
Enter 0 to exit
1
Enter the roll number of the student to be added-
101
Enter the name of the student to be added-
John Doe
Enter the division of the student to be added-
A
Enter the address of the student to be added-
123 Main Street
-----STUDENT RECORDS-----
Enter 1 for adding a record
Enter 2 for displaying the records
Enter 3 for searching a record
Enter 4 for deleting a record
Enter 0 to exit
2
Roll Number- 101
Name- John
Division- A
Address- 123 Main Street
-----STUDENT RECORDS-----
Enter 1 for adding a record
Enter 2 for displaying the records
Enter 3 for searching a record
Enter 4 for deleting a record
Enter 0 to exit
3
Enter roll number of the student to be searched-
101
Student's record found!
-----STUDENT RECORDS-----
Enter 1 for adding a record
Enter 2 for displaying the records
Enter 3 for searching a record
Enter 4 for deleting a record
Enter 0 to exit
4
Enter roll number of the student to be deleted-
101
Student record to be deleted is deleted successfully!
-----STUDENT RECORDS-----
Enter 1 for adding a record
Enter 2 for displaying the records
Enter 3 for searching a record
Enter 4 for deleting a record
Enter 0 to exit
0
EXPLANATION -
This C++ program manages student records by allowing users to add, display, search, and delete records. Let's review its functionality:
1. **Student Struct**:
- It defines a `Student` struct with attributes for roll number, name, division, and address.
2. **Add Students Function** (`add_students()`):
- Prompts the user to input student details (roll number, name, division, address).
- Appends the entered details to a file named "students.txt".
3. **Display Function** (`display()`):
- Displays the details of a single student.
4. **Read Students Function** (`read_students()`):
- Reads student records from the "students.txt" file and displays them.
5. **Search Students Function** (`search_students()`):
- Searches for a student record by roll number.
- Returns the roll number if found, otherwise, returns -1.
6. **Delete Students Function** (`delete_students()`):
- Deletes a student record by roll number.
- Creates a temporary file to store records except the one to be deleted.
- Renames the temporary file to replace the original file.
7. **Main Function**:
- Presents a menu to the user with options to add, display, search, or delete records.
- Reads the user's choice and calls the corresponding function.
- Loops until the user chooses to exit (enters 0).
This program effectively manages student records through file handling operations, providing basic CRUD (Create, Read, Update, Delete) functionalities. However, there are some improvements that can be made, such as error handling and more robust file input/output operations.
*/