-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
64 lines (58 loc) · 1.5 KB
/
database.cpp
File metadata and controls
64 lines (58 loc) · 1.5 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
#include "Database.hpp"
#include <iostream>
#include <fstream>
using namespace std;
void Database::loadFromFile() {
ifstream file(FILENAME);
if (!file.is_open()) return;
string name, phone;
while (file >> name >> phone) {
data[name] = phone;
}
file.close();
}
void Database::saveToFile() {
ofstream file(FILENAME);
for (auto& pair : data) {
file << pair.first << " " << pair.second << "\n";
}
file.close();
}
void Database::addData() {
string name, phone;
cout << "Enter name: ";
cin >> name;
cout << "Enter phone: ";
cin >> phone;
data[name] = phone;
saveToFile();
cout << "✓ Saved: " << name << " → " << phone << "\n";
}
void Database::findData() {
string name;
cout << "Enter name to find: ";
cin >> name;
if (data.count(name)) {
cout << "Found: " << name << " → " << data[name] << "\n";
} else {
cout << "✗ Not found.\n";
}
}
void Database::run() {
loadFromFile();
int choice;
do {
cout << "\n=== SimpleDB ===\n";
cout << "1. Add Data\n";
cout << "2. Find Data\n";
cout << "3. Exit\n";
cout << "Choice: ";
cin >> choice;
switch (choice) {
case 1: addData(); break;
case 2: findData(); break;
case 3: cout << "Goodbye!\n"; break;
default: cout << "Invalid choice.\n";
}
} while (choice != 3);
}