Repository navigation
Expand file tree
/
Copy pathnotes.cpp
More file actions
162 lines (144 loc) · 3.97 KB
/
Copy pathnotes.cpp
File metadata and controls
162 lines (144 loc) · 3.97 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <bits/stdc++.h>
using namespace std;
struct Note {
int id;
string title;
string content;
string tags;
};
vector<Note> notes;
const string FILENAME = "notes.txt";
void loadNotes() {
notes.clear();
ifstream fin(FILENAME);
if (!fin) return;
Note n;
while (fin >> n.id) {
fin.ignore();
getline(fin, n.title);
getline(fin, n.content);
getline(fin, n.tags);
notes.push_back(n);
}
fin.close();
}
void saveNotes() {
ofstream fout(FILENAME);
for (auto &n : notes) {
fout << n.id << "\n";
fout << n.title << "\n";
fout << n.content << "\n";
fout << n.tags << "\n"; // NEW LINE
}
fout.close();
}
int getNextId() {
int mx = 0;
for (auto &n : notes) mx = max(mx, n.id);
return mx + 1;
}
int findIndexById(int id) {
for (int i = 0; i < (int)notes.size(); i++) {
if (notes[i].id == id) return i;
}
return -1;
}
void createNote() {
Note n;
n.id = getNextId();
cout << "Enter title: ";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, n.title);
cout << "Enter content: ";
getline(cin, n.content);
cout << "Enter tags (comma-separated): ";
getline(cin, n.tags);
notes.push_back(n);
saveNotes();
cout << "Note added!\n";
}
void viewNotes() {
if (notes.empty()) {
cout << "No notes found.\n";
return;
}
for (auto &n : notes) {
cout << "\nID: " << n.id << "\nTitle: " << n.title << "\nContent: " << n.content << "\n";cout << "Tags: " << n.tags << "\n";
}
}
void searchNotes() {
cout << "Enter keyword: ";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string key;
getline(cin, key);
bool found = false;
for (auto &n : notes) {
if (n.title.find(key) != string::npos || n.content.find(key) != string::npos) {
cout << "\nID: " << n.id << "\nTitle: " << n.title << "\nContent: " << n.content << "\n";
n.tags.find(key) != string::npos;
}
}
if (!found) cout << "No matching notes.\n";
}
void editNote() {
cout << "Enter note ID to edit: ";
int id;
cin >> id;
int idx = findIndexById(id);
if (idx == -1) {
cout << "Note not found.\n";
return;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // clear buffer
Note &n = notes[idx];
cout << "Current title: " << n.title << "\n";
cout << "Enter new title (leave empty to keep): ";
string newTitle;
getline(cin, newTitle);
if (!newTitle.empty()) n.title = newTitle;
cout << "Current content: " << n.content << "\n";
cout << "Enter new content (leave empty to keep): ";
string newContent;
getline(cin, newContent);
if (!newContent.empty()) n.content = newContent;
cout << "Current tags: " << n.tags << "\n";
cout << "Enter new tags (leave empty to keep): ";
string newTags;
getline(cin, newTags);
if (!newTags.empty()) n.tags = newTags;
saveNotes();
cout << "Note updated.\n";
}
void deleteNote() {
cout << "Enter note ID to delete: ";
int id;
cin >> id;
for (int i = 0; i < notes.size(); i++) {
if (notes[i].id == id) {
notes.erase(notes.begin() + i);
saveNotes();
cout << "Note deleted!\n";
return;
}
}
cout << "Note not found.\n";
}
int main() {
loadNotes();
int choice;
while (true) {
cout << "\n=== Personal Knowledge Vault ===\n";
cout << "1. Create Note\n2. View Notes\n3. Search Notes\n4. Edit Note\n5. Delete Note\n6. Exit\n";
cout << "Choose option: ";
cin >> choice;
switch (choice) {
case 1: createNote(); break;
case 2: viewNotes(); break;
case 3: searchNotes(); break;
case 4: editNote(); break;
case 5: deleteNote(); break;
case 6: cout << "Goodbye!\n"; return 0;
default: cout << "Invalid option.\n";
}
}
}