-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTalkSpace.cpp
More file actions
155 lines (131 loc) · 3.97 KB
/
TalkSpace.cpp
File metadata and controls
155 lines (131 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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
const string FILE_NAME = "chat_history.txt";
// Load chat history from file
void loadChatHistory(vector<string>& chatHistory) {
ifstream file(FILE_NAME);
string line;
while (getline(file, line)) {
chatHistory.push_back(line);
}
file.close();
}
// Save chat history to file
void saveChatHistory(const vector<string>& chatHistory) {
ofstream file(FILE_NAME);
for (const string& msg : chatHistory) {
file << msg << endl;
}
file.close();
}
// Display chat history
void displayChatHistory(const vector<string>& chatHistory) {
cout << "\n--- Chat History ---" << endl;
if (chatHistory.empty()) {
cout << "No messages yet." << endl;
} else {
for (const string& msg : chatHistory) {
cout << msg << endl;
}
}
cout << "--------------------" << endl;
}
// Clear chat history
void clearChatHistory(vector<string>& chatHistory) {
chatHistory.clear();
saveChatHistory(chatHistory);
cout << "Chat history cleared." << endl;
}
// Search messages
void searchMessages(const vector<string>& chatHistory) {
string keyword;
cout << "Enter keyword to search: ";
getline(cin, keyword);
bool found = false;
cout << "\n--- Search Results ---" << endl;
for (const string& msg : chatHistory) {
if (msg.find(keyword) != string::npos) {
cout << msg << endl;
found = true;
}
}
if (!found) {
cout << "No matching messages found." << endl;
}
cout << "----------------------" << endl;
}
// Edit last message
void editLastMessage(vector<string>& chatHistory, const string& currentUser) {
for (int i = chatHistory.size() - 1; i >= 0; i--) {
if (chatHistory[i].find(currentUser + ": ") == 0) {
string newMsg;
cout << "Enter new message: ";
getline(cin, newMsg);
chatHistory[i] = currentUser + ": " + newMsg;
saveChatHistory(chatHistory);
cout << "Message edited." << endl;
return;
}
}
cout << "No message found to edit." << endl;
}
// Delete last message
void deleteLastMessage(vector<string>& chatHistory, const string& currentUser) {
for (int i = chatHistory.size() - 1; i >= 0; i--) {
if (chatHistory[i].find(currentUser + ": ") == 0) {
chatHistory.erase(chatHistory.begin() + i);
saveChatHistory(chatHistory);
cout << "Message deleted." << endl;
return;
}
}
cout << "No message found to delete." << endl;
}
int main() {
vector<string> chatHistory;
string message;
string user1, user2;
// Load old messages
loadChatHistory(chatHistory);
cout << "Enter name for User 1: ";
getline(cin, user1);
while (true) {
cout << "Enter name for User 2: ";
getline(cin, user2);
if (user2 != user1) break;
cout << "Username already used.\n";
}
string currentUser = user1;
cout << "\n\t\t\t TalkSpace" << endl;
cout << "Commands: exit | clear | edit | delete | search" << endl;
while (true) {
displayChatHistory(chatHistory);
cout << currentUser << ": ";
getline(cin, message);
if (message == "exit") {
break;
}
else if (message == "clear") {
clearChatHistory(chatHistory);
}
else if (message == "search") {
searchMessages(chatHistory);
}
else if (message == "edit") {
editLastMessage(chatHistory, currentUser);
}
else if (message == "delete") {
deleteLastMessage(chatHistory, currentUser);
}
else {
chatHistory.push_back(currentUser + ": " + message);
saveChatHistory(chatHistory);
currentUser = (currentUser == user1) ? user2 : user1;
}
}
cout << "Chat ended." << endl;
return 0;
}