-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata
More file actions
105 lines (96 loc) · 2.72 KB
/
data
File metadata and controls
105 lines (96 loc) · 2.72 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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Student {
int id;
string name;
int age;
};
void addRecord(const Student& student) {
ofstream file("students.txt", ios::app);
if (file.is_open()) {
file << student.id << " " << student.name << " " << student.age << endl;
file.close();
cout << "Record added successfully." << endl;
} else {
cout << "Unable to open file." << endl;
}
}
void searchRecord(int id) {
ifstream file("students.txt");
if (file.is_open()) {
int studentId;
string name;
int age;
bool found = false;
while (file >> studentId >> name >> age) {
if (studentId == id) {
found = true;
cout << "Student ID: " << studentId << ", Name: " << name << ", Age: " << age << endl;
break;
}
}
if (!found) {
cout << "Record not found." << endl;
}
file.close();
} else {
cout << "Unable to open file." << endl;
}
}
void displayAllRecords() {
ifstream file("students.txt");
if (file.is_open()) {
int studentId;
string name;
int age;
while (file >> studentId >> name >> age) {
cout << "Student ID: " << studentId << ", Name: " << name << ", Age: " << age << endl;
}
file.close();
} else {
cout << "Unable to open file." << endl;
}
}
int main() {
int choice;
Student newStudent;
do {
cout << "\nDatabase Menu\n";
cout << "1. Add Record\n";
cout << "2. Search Record\n";
cout << "3. Display All Records\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter student ID: ";
cin >> newStudent.id;
cout << "Enter student name: ";
cin.ignore();
getline(cin, newStudent.name);
cout << "Enter student age: ";
cin >> newStudent.age;
addRecord(newStudent);
break;
case 2:
int searchId;
cout << "Enter student ID to search: ";
cin >> searchId;
searchRecord(searchId);
break;
case 3:
cout << "All Records:" << endl;
displayAllRecords();
break;
case 4:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 4);
return 0;
}