feat: replace exit with ctrl+d

This commit is contained in:
2026-01-14 07:43:40 +01:00
parent d1f6eae102
commit 16fe1178f4

View File

@@ -25,22 +25,15 @@ const map<char, string> morse_map = {
bool should_exit = false; bool should_exit = false;
bool is_exit(const string &s) {
return s.length() == 4 && tolower(static_cast<unsigned char>(s[0])) == 'e' &&
tolower(static_cast<unsigned char>(s[1])) == 'x' &&
tolower(static_cast<unsigned char>(s[2])) == 'i' &&
tolower(static_cast<unsigned char>(s[3])) == 't';
}
string read_input(const string &prompt) { string read_input(const string &prompt) {
cout << prompt; cout << prompt;
string line; string line;
if (!getline(cin, line) || line.empty()) if (!getline(cin, line)) {
return "";
if (is_exit(line)) {
should_exit = true; should_exit = true;
return ""; return "";
} }
if (line.empty())
return "";
return line; return line;
} }
@@ -119,7 +112,7 @@ public:
void decode_mode() { void decode_mode() {
MorseDecoder decoder; MorseDecoder decoder;
cout << "Decode mode: Enter morse sequence to decode. Press 'e' to switch to " cout << "Decode mode: Enter morse sequence to decode. Press 'e' to switch to "
"encode mode, 'exit' to exit.\n\n"; "encode mode.\n\n";
while (!should_exit) { while (!should_exit) {
string input = read_input("Enter morse sequence: "); string input = read_input("Enter morse sequence: ");
@@ -171,7 +164,7 @@ void decode_mode() {
void encode_mode() { void encode_mode() {
MorseEncoder encoder; MorseEncoder encoder;
cout << "Encode mode: Enter text to see Morse code patterns. Press 'd' to " cout << "Encode mode: Enter text to see Morse code patterns. Press 'd' to "
"switch to decode mode, 'exit' to exit.\n\n"; "switch to decode mode.\n\n";
while (!should_exit) { while (!should_exit) {
string input = read_input("Enter text to encode: "); string input = read_input("Enter text to encode: ");