-
Notifications
You must be signed in to change notification settings - Fork 0
/
Diary.cpp
317 lines (275 loc) · 8.05 KB
/
Diary.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <vector>
#include <limits>
using namespace std;
struct DiaryEntry
{
string subject;
string date;
string content;
string password;
string passwordHint;
};
const string diaryFileName = "MyDiary.txt";
// Function to validate the password
bool validatePassword(const string &inputPassword, const string &storedPassword)
{
return inputPassword == storedPassword;
}
// Function to show password hint
void showPasswordHint(const string &hint)
{
cout << "Password hint: " << hint << endl;
}
// Function to load diary entries from a file
vector<DiaryEntry> loadDiaryEntries()
{
ifstream file(diaryFileName);
vector<DiaryEntry> diary;
if (!file)
{
return diary;
}
DiaryEntry entry;
while (getline(file, entry.subject))
{
getline(file, entry.date);
getline(file, entry.content);
getline(file, entry.password);
getline(file, entry.passwordHint);
diary.push_back(entry);
}
file.close();
return diary;
}
// Function to save diary entries to a file
void saveDiaryEntries(const vector<DiaryEntry> &diary)
{
ofstream file(diaryFileName);
if (!file)
{
cerr << "Failed to open the diary file." << endl;
return;
}
for (const DiaryEntry &entry : diary)
{
file << entry.subject << '\n'
<< entry.date << '\n'
<< entry.content << '\n'
<< entry.password << '\n'
<< entry.passwordHint << '\n';
}
file.close();
}
// Function to add a new diary entry
void addDiaryEntry(vector<DiaryEntry> &diary)
{
DiaryEntry entry;
cin.ignore(); // Consume the newline character
cout << "Enter the subject of the diary: ";
getline(cin, entry.subject);
// Get current date and time
time_t now = time(0);
tm *currentDate = localtime(&now);
char dateBuffer[80];
strftime(dateBuffer, 80, "%Y-%m-%d %H:%M:%S", currentDate);
entry.date = dateBuffer;
cout << "Enter the content of the diary (press Enter on an empty line to finish):\n";
string line;
while (true)
{
getline(cin, line);
if (line.empty())
{
break;
}
entry.content += line + '\n';
}
cout << "Do you want to protect this diary with a password? (y/n): ";
char protectChoice;
cin >> protectChoice;
if (protectChoice == 'y' || protectChoice == 'Y')
{
string password, confirmPassword, hint;
cout << "Enter a password: ";
cin >> password;
cout << "Confirm the password: ";
cin >> confirmPassword;
if (password != confirmPassword)
{
cout << "Password confirmation failed. Diary not protected." << endl;
}
else
{
entry.password = password;
cin.ignore(); // Consume the newline character
cout << "Enter a password hint: ";
getline(cin, hint);
entry.passwordHint = hint;
}
}
diary.push_back(entry);
saveDiaryEntries(diary); // Save the updated diary to the file
}
// Function to view diary entries
void viewDiaryEntries(const vector<DiaryEntry> &diary)
{
vector<DiaryEntry> loadedDiary = loadDiaryEntries();
if (loadedDiary.empty())
{
cout << "No diary entries found." << endl;
return;
}
cout << "Diary Entries:" << endl;
for (size_t i = 0; i < loadedDiary.size(); ++i)
{
cout << i + 1 << ". " << loadedDiary[i].subject << " (" << loadedDiary[i].date << ")" << endl;
}
cout << "--------------------------" << endl;
cout << "Enter the number of the diary entry to view: ";
int entryNumber;
cin >> entryNumber;
if (entryNumber > 0 && entryNumber <= loadedDiary.size())
{
const DiaryEntry &entry = loadedDiary[entryNumber - 1];
string inputPassword;
if (entry.password.empty())
{
cout << entry.content << endl;
}
else
{
cout << "This diary is password protected. Enter the password: ";
cin >> inputPassword;
if (validatePassword(inputPassword, entry.password))
{
cout << entry.content << endl;
}
else
{
showPasswordHint(entry.passwordHint);
}
}
}
else
{
cout << "Invalid entry number." << endl;
}
}
// Function to delete a diary entry
void deleteDiaryEntry(vector<DiaryEntry> &diary)
{
if (diary.empty())
{
cout << "No diary entries to delete." << endl;
return;
}
cout << "Diary Entries:" << endl;
for (size_t i = 0; i < diary.size(); ++i)
{
cout << i + 1 << ". " << diary[i].subject << " (" << diary[i].date << ")" << endl;
}
cout << "Enter the number of the diary entry to delete: ";
int entryNumber;
cin >> entryNumber;
if (entryNumber > 0 && entryNumber <= diary.size())
{
DiaryEntry &entry = diary[entryNumber - 1];
if (!entry.password.empty())
{
string inputPassword;
cout << "This diary is password protected. Enter the password to delete: ";
cin >> inputPassword;
if (!validatePassword(inputPassword, entry.password))
{
showPasswordHint(entry.passwordHint);
cout << "Password is incorrect. Deletion aborted." << endl;
return;
}
}
diary.erase(diary.begin() + entryNumber - 1);
saveDiaryEntries(diary); // Save the updated diary to the file
cout << "Diary entry deleted successfully." << endl;
}
else
{
cout << "Invalid entry number." << endl;
}
}
// Function to search diary entries by subject
void searchDiaryEntries(const vector<DiaryEntry> &diary)
{
cin.ignore(); // Consume the newline character
cout << "Enter the subject to search for: ";
string searchTerm;
getline(cin, searchTerm);
bool found = false;
for (const DiaryEntry &entry : diary)
{
if (entry.subject.find(searchTerm) != string::npos)
{
cout << "Subject: " << entry.subject << " (" << entry.date << ")" << endl;
if (entry.password.empty())
{
cout << "Content: " << entry.content << endl;
}
else
{
cout << "This diary is password protected. To view its content, use the 'View diary entries' feature and enter the password." << endl;
}
cout << "-------------------" << endl;
found = true;
}
}
if (!found)
{
cout << "No diary entries found with the specified subject." << endl;
}
}
int main()
{
vector<DiaryEntry> diary = loadDiaryEntries();
while (true)
{
cout << "--------------------------" << endl;
cout << "Diary Program Menu:" << endl;
cout << "1. Add new diary entry" << endl;
cout << "2. View diary entries" << endl;
cout << "3. Delete diary entry" << endl;
cout << "4. Search diary entries" << endl;
cout << "5. Quit" << endl;
int choice;
cout << "-----------------------------------" << endl;
cout << "Enter your choice -> ";
cin >> choice;
cout << "-----------------------------------" << endl;
switch (choice)
{
case 1:
addDiaryEntry(diary);
break;
case 2:
viewDiaryEntries(diary);
break;
case 3:
deleteDiaryEntry(diary);
break;
case 4:
searchDiaryEntries(diary);
break;
case 5:
cout << "---- Exiting the diary program ----" << endl;
saveDiaryEntries(diary);
return 0;
default:
cout << "Invalid choice. Please enter a valid option (1-5)." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
break;
}
}
return 0;
}