-4

I want to find out how to go from morse code back to text. Morse code must be read in with this format .../---/... each character is separated by a '/'. I figure you would have to split up each code and store it in a temp string to then find its english variable then reset the temp string

#include <iostream>
#include <cstring>
using namespace std;

void displayMenu();
char menuChoice();
string toMorse(char);

const int NumChar = 26;

string morse[NumChar] = {
    ".-", "-...","-.-.", "-..", ".", "..-.",
    "--.", "....", "..", ".---",
    "-.-", ".-..", "--", "-.",
    "---", ".--.", "--.-", ".-.",
    "...", "-", "..-", "...-",
    ".--", "-..-", "-.--", "--.."
};

int main()
{
    cout<<"This is the Morse Code Converter."<<endl;
    char menu = 0, c;
    string str;
    do{
        switch(menu){
            case 'A':
                cout<<"Enter a word and I will translate it to Morse Code: \n";
                toMorse(c);
                break;
            case 'B':
                cout<<"Enter a Morse Code separated by /s and I will translate it to text.\n";
                break;
        }
        menu=menuChoice();
    }while(menu!='C');

    return 0;
}

void displayMenu(){
    cout<<"A) Text to Morse code"<<endl;
    cout<<"B) Morse code to text"<<endl;
    cout<<"C) Quit"<<endl;
}
char menuChoice(){
    char menu;
    displayMenu();
    cout<<"Pick Choice: ";
    cin>>menu;
    menu=toupper(menu);
    cin.ignore(1,'\n');

    while((menu<'A')||(menu>'C')){
        displayMenu();
        cout<<"Enter in a proper choice: ";
        cin>>menu;
        menu= toupper(menu);
        cin.ignore(1,'\n');
    }
        return menu;
}
string toMorse(char) {
    char letter[100];
    cin>>letter;
    int error=0;
    for (int i=0;i<strlen(letter);i++){
        letter[i]=toupper(letter[i]);
        for (int j=0;j<26;j++){
            if ( int(letter[i])-65 == j){
                error=1;
                cout<<morse[int(letter[i])-65]<<endl;
                break;
            }
        }
        if (error==0){
            cout<<"Error : word contains symbols"<<endl;
            break;
        }
    }
}

this is the part I am stuck on right now, I know what I'm supposed to do but i don' know how to write it in the code.

string toEnglish(char,string) {

}
Eduardo Pascual Aseff
  • 1,118
  • 2
  • 9
  • 22
  • 2
    what are the two parameters? Input is a string with the morse, but what is the `char`? – 463035818_is_not_a_number Apr 09 '20 at 22:00
  • There happens to be a fairly simple way to do this kind of a thing. Just take out a blank sheet of paper, and write down using brief, simple sentences in plain English, a step-by-step process of doing this. When done, [schedule an emergency appointment with your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). After your rubber duck approves your proposed plan of action, simply take what you've written down and translate it directly into C++. Mission accomplished! – Sam Varshavchik Apr 09 '20 at 22:02
  • 1
    Have you considered using a `std::unordered_map`. where the string is the morse code, and the char is the English character that corresponds with the code? That would reduce the code down tremendously. – PaulMcKenzie Apr 09 '20 at 22:03
  • you already have the array for the mapping, You can use `std::find` and transform its result to the desired character – 463035818_is_not_a_number Apr 09 '20 at 22:05
  • @idclev463035818 I don't know what the ```char``` is or either, my professor had it there, but i don't think i need it. – Jordan Northup Apr 09 '20 at 22:07
  • A morse code to English function should just have a single argument, the morse code string, and a single output, the English string. That `char` argument serves no purpose. – PaulMcKenzie Apr 09 '20 at 22:09
  • Well, if you have a question about your professor's assignment, doesn't it make logical sense to ask your professor for clarification? After all, it's their job, to help you learn. They get paid to do this, so your professor would be the best resource for questions about your assignment. – Sam Varshavchik Apr 09 '20 at 22:09
  • @SamVarshavchik well that does work, i just don't know how to translate what i want into code. i also asked her, didn't help much – Jordan Northup Apr 09 '20 at 22:10
  • @PaulMcKenzie yeah i figured that , it just doesn't fit – Jordan Northup Apr 09 '20 at 22:11
  • @JordanNorthup -- Well, as a favor and to anyone who has searched and landed on this question [here is an example of using a map](http://coliru.stacked-crooked.com/a/0cc502d7edd844af) – PaulMcKenzie Apr 09 '20 at 22:23
  • Thanks I'll remember that, too bad we can't use that for this assignment though, it'd be much easier. – Jordan Northup Apr 09 '20 at 22:26
  • See [this](https://stackoverflow.com/a/59017704/1983409) answer. – zdf Apr 09 '20 at 23:20
  • Here is a Morse code emitter local https://codereview.stackexchange.com/a/71246/507 I am sure you could reverse it to get an inputter. – Martin York Apr 09 '20 at 23:52

2 Answers2

0

I recommend you use a mapping table, which is an array or std::vector of pairs:

struct Key_Value
{
    const * morse;
    char    alpha;
};

struct Key_Value    conversion_table[] =
{
    {".", 'E'},
    {"-", 'T'},
};
unsigned int size_conversion_table =
    sizeof(conversion_table) / sizeof(conversion_table[0]);

If you input the Morse "word", you can go through the table comparing the word to the "morse" field of the row. If there is a match, use the alpha field to get the character.

Example:

std::string morse_letter = ".";
bool found = false;
char english_letter = '*';
for (unsigned int i = 0; i < size_conversion_table; ++i)
{
    std::string table_string = conversion_table[i].morse;
    if (morse_letter == table_string)
    {
        english_letter = conversion_table[i].alpha;
        found = true;
        break;
    }
}  

One nice feature of the table lookup is that you can get the code working for 2 entries, then add more entries without having to change the code.

You can use the same table for mapping English letters to Morse code.

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144
-1

Using a map would be simpler, but as you already have the array with the morse alphabet you can use that. You can use std::find to find the morse in the array and transform its result to the character:

#include <string>
#include <iostream>
#include <algorithm>


std::string morse[] = {".-", "-...","-.-.", "-..", ".", "..-.",
"--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.",
"---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-",
".--", "-..-", "-.--", "--.."
};

char get_char(std::string m) {
    auto it = std::find(std::begin(morse),std::end(morse),m);
    if (it == std::end(morse)) return '0';
    return 'a' + std::distance(std::begin(morse),it);
}

int main () {
    std::cout << get_char("..-.") << get_char("---") << get_char("---");
}

Output:

foo

PS: If the function you are looking for is supposed to transform a complete string of many morse codes to a english string then I misunderstood your question and it would require a bit more input from your side.

463035818_is_not_a_number
  • 64,173
  • 8
  • 58
  • 126
  • I understand that using a map would be simpler, but my professor said we can only use the given libraries. – Jordan Northup Apr 09 '20 at 22:13
  • @JordanNorthup please look at the answer, I am not using a map – 463035818_is_not_a_number Apr 09 '20 at 22:14
  • @JordanNorthup it is shorthand for the type returned by `std::find`, I think it is a `string*` but I would have to look it up – 463035818_is_not_a_number Apr 09 '20 at 22:18
  • oh okay. and also so what i have to do is read in a morse code phrase like .../---/... (SOS) and have the function take each morse letter and find the corresponding english letter. But it has to be the morse code must be input using /s as separators. That's the part thats confusing me – Jordan Northup Apr 09 '20 at 22:23
  • @JordanNorthup what is confusing you? You just need to split the string using `/` as delimiter and then you can apply what I wrote in my answer. (see here for splitting: https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c) – 463035818_is_not_a_number Apr 09 '20 at 22:25
  • @JordanNorthup dont get me wrong, but if you struggle with something you shouldn't write "I know what I'm supposed to do" but instead explain what makes you struggle ;) – 463035818_is_not_a_number Apr 09 '20 at 22:27
  • fair enough. programming is a lot harder than i thought it would be. I'm already retaking this course for the second time because i failed the first time. – Jordan Northup Apr 09 '20 at 22:32