-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent_management.cpp
More file actions
143 lines (122 loc) · 3.81 KB
/
Copy pathstudent_management.cpp
File metadata and controls
143 lines (122 loc) · 3.81 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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student {
private:
string name, roll_no, course, address;
public:
void menu();
void insert();
void display();
void modify();
void search();
void del();
};
void Student::menu() {
int choice;
while (true) {
cout << "\n\t--- STUDENT MANAGEMENT SYSTEM ---\n";
cout << "1. Add Record\n2. Display All\n3. Modify\n4. Search\n5. Delete\n6. Exit\n";
cout << "Enter Choice: ";
cin >> choice;
if (choice == 6) break;
switch (choice) {
case 1: insert(); break;
case 2: display(); break;
case 3: modify(); break;
case 4: search(); break;
case 5: del(); break;
default: cout << "Invalid choice!\n";
}
}
}
void Student::insert() {
cin.ignore();
cout << "Name: "; getline(cin, name);
cout << "Roll No: "; getline(cin, roll_no);
cout << "Course: "; getline(cin, course);
cout << "Address: "; getline(cin, address);
ofstream file("studentRecord.txt", ios::app);
file << name << "|" << roll_no << "|" << course << "|" << address << "\n";
file.close();
cout << "Record Saved!\n";
}
void Student::display() {
ifstream file("studentRecord.txt");
if (!file) { cout << "No data found!\n"; return; }
cout << "\n--- All Students ---\n";
while (getline(file, name, '|') &&
getline(file, roll_no, '|') &&
getline(file, course, '|') &&
getline(file, address)) {
cout << "Roll: " << roll_no << " | Name: " << name << " | Course: " << course << endl;
}
file.close();
}
void Student::search() {
string target;
cout << "Enter Roll No to search: ";
cin >> target;
ifstream file("studentRecord.txt");
bool found = false;
while (getline(file, name, '|') && getline(file, roll_no, '|') &&
getline(file, course, '|') && getline(file, address)) {
if (roll_no == target) {
cout << "Found! Name: " << name << ", Course: " << course << endl;
found = true;
break;
}
}
file.close();
if (!found) cout << "Student not found.\n";
}
void Student::del() {
string target;
cout << "Enter Roll No to delete: ";
cin >> target;
ifstream file("studentRecord.txt");
ofstream temp("temp.txt");
while (getline(file, name, '|') && getline(file, roll_no, '|') &&
getline(file, course, '|') && getline(file, address)) {
if (roll_no != target) {
temp << name << "|" << roll_no << "|" << course << "|" << address << "\n";
}
}
file.close();
temp.close();
remove("studentRecord.txt");
rename("temp.txt", "studentRecord.txt");
cout << "Process completed.\n";
}
void Student::modify() {
string target;
cout << "Enter Roll No to modify: ";
cin >> target;
ifstream file("studentRecord.txt");
ofstream temp("temp.txt");
bool found = false;
while (getline(file, name, '|') && getline(file, roll_no, '|') &&
getline(file, course, '|') && getline(file, address)) {
if (roll_no == target) {
cin.ignore();
cout << "New Name: "; getline(cin, name);
cout << "New Course: "; getline(cin, course);
cout << "New Address: "; getline(cin, address);
temp << name << "|" << roll_no << "|" << course << "|" << address << "\n";
found = true;
} else {
temp << name << "|" << roll_no << "|" << course << "|" << address << "\n";
}
}
file.close();
temp.close();
remove("studentRecord.txt");
rename("temp.txt", "studentRecord.txt");
cout << (found ? "Record modified!\n" : "Student not found.\n");
}
int main() {
Student s;
s.menu();
return 0;
}