Morse to English Translator

I've written my code and it works perfect for converting english to morse! I'm having difficulty figuring out how to write the code so that the user can input morse and it can be translated back to english. Thanks!

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
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>

using namespace std;

//function prototypes
string morse(char m);
void englishMorse();
void  morseEnglish();
string lowercase(string);

int main(){

cout << "Welcome to the Message Encryptor!" <<endl;
cout << "For English to Morse Code, press 1." << endl;
cout << "For Morse to English, press 2." << endl;
        int a;
        cin >> a;

    if(a == 1){
    englishMorse();    //passes to englishMorse function to encrypt message
        }

    else if(a == 2){
    morseEnglish();    //passes to morseEnglish function to decrypt message
        }

return 0;
}

//function with array for morse code
string morse(char m){

    string alphabet = "abcdefghijklmnopqrstuvwxyz";
    string morse[] = {"*-","-***","-*-*","-**","*","**-*","--*","****",
    "**","*---","-*-","*-**","--","-*","---","*--*","--*-", "*-*","***",
    "-","**-","***-","*--","-**-","-*--","--*"};

    int finder = alphabet.find(m);
        if(finder!=-1)
            return morse[finder];
        else
            return "XX";  
}  



//function to convert user input into morse code
void englishMorse(){
   cin.ignore(); 
    cout << "Enter a message you'd like to encrypt :" << endl;
        string message;
        getline(cin, message);
        string phrase = "";
            lowercase(message);
    for(int i=0; i<message.length(); i++){
        if (i==0){
        phrase = morse(message[i]) + "X";;
       }
        else {
        phrase += morse(message[i]) + "X";
      }

    }
    cout << phrase << endl;  
}

//function to decrypt user's morse code 
void morseEnglish(){
    cin.ignore(); //to ignore everything around the string it is calling from getline
    cout << "Enter the code you'd like to decrypt: " << endl;
        string message;
        getline(cin, message);
        string phrase = "";
        
    for(int i=0; i<message.length(); i++){
        phrase += (message[i]) + "";
        }
    cout << phrase << endl;
}

//function to convert any user input to lower case
string lowercase (string message){

    for(int x=0; x < message.length(); x++){
        if(65 >= (int)message[x] or (int)message[x] <= 90) {
        message[x] += 32;
        }
    }
        return message;
}
What is your problem? For me it seems to work.
I'm having trouble translating morse into english (morseEnglish function).
You need to split the input into morse tokens and translate token for token - like you did in english to morse just the other way around. Since a morse token can be between 2 and 4 characters long you need to specify a separator.
Line 79: I'm not clear what you're trying to do here.

What you need here is a loop that scans message for a space. When you find a space, you have a substring that is one morse token. Then you're going to need a complement to your morse function that takes a Morse token and looks up the matching alpha character.

Edit: I like Thomas1965's term "Morse token" better than "Morse character" which I originally used.

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
char morse_to_alpha (const string & tkn)
{   if (tkn.empty())
       return ' ';
    for (int i=0; i<26; i++)
        if (morse[i] == tkn)
            return alphabet[i];
    return '*';     //  Couldn't translate
}

void morseEnglish()
{   string  message;
    string  token;
    string  result;
        
    cout << "Enter a message you'd like to convert from Morse to English:" << endl;
    getline(cin, message);
    message += ' ';   // Force a trailing space
    for (int i=0; i<message.length(); i++)
    {   if (message[i] == ' ')
        {   // end of a morse token
             result += morse_to_alpha (token);
            token.clear ();
        }
        else
            token += message[i];
    }
    cout << result << endl;  
}


The challenge is going to be handling spaces in the Morse message. You're going to need to keep track of the number of consecutive spaces. If you find only one space, then you know you have the end of a morse character. If you find two or more spaces between Morse characters, then you have a space between alpha words.

Don't forget that Morse code also includes numbers.
https://en.wikipedia.org/wiki/Morse_code
Last edited on
Topic archived. No new replies allowed.