Morse Code

Pages: 12
closed account (48T7M4Gy)
Yes VR this is a prime example where an STL container, in this case a <map> cuts out at least 50% of the code simply because the map does all the work.

( It also depends on how the base data is stored, hard-coded or as a separate file or as structs as you can see. But it doesn't need to be complicated due to the nature of maps. )
Well I updated my code to use maps, I chose not to go the external data file route because I used cpp.sh to write all the code since I am playing around with this at work (shhhhhhh).

So, here is the updated code...

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
#include <iostream>
#include <map>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

std::map <char, std::string> morseList;
std::map <std::string, char> asciiList;

//function prototypes
void displaymorse();
void load_morse();
void load_ascii_from_morse();
void englishtomorse();
void morsetoenglish();
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems);
std::vector<std::string> split(const std::string &s, char delim);

int main()
{
    int choice=1;

    load_morse();
    load_ascii_from_morse ();
    
    std::cout << "This programm will convert a letter, number, word or phrase\n";
    std::cout << "from english to morse code, or from morse code to english.\n";
    std::cout << "Only letters and numbers are converted, others are ignored,\n";
    std::cout << "all letters are converted to lowercase automatically.\n";
    std::cout << "\n";
    
    while (choice != 0)
    {
        std::cout << "What would you like to do?\n";
        std::cout << "Enter 0 to exit...\n";
        std::cout << "Enter 1 to display the morse code table...\n";
        std::cout << "Enter 2 for english to morse code...\n";
        std::cout << "Enter 3 for morse code to english...\n";
        std::cout << "Enter your choice: ";

        std::cin >> choice;

        //if user entered something besides a number we handle it here
        //after clearing the error we set choice to 4 to inform the user
        //of an invalid entry and ask them to try again
        if(!std::cin)
        {
            std::cin.clear();
            std::cin.ignore(999,'\n');
            choice = 4;
        }
            
        std::cout << "\n";

        switch (choice)
        {
            case 0:
                std::cout << "\nGood bye\n\n\n";
                exit(0);
                break;
            case 1:
                displaymorse();
                break;
            case 2:
                englishtomorse();
                break;
            case 3:
                displaymorse();
                morsetoenglish();
                break;
            default:
                std::cout << "\nInvalid choice, please try again.\n\n\n";
                break;
        }
    }

    return 0;
}

void load_morse ()
{
    morseList[' '] = " ";
    morseList['0'] = "-----";
    morseList['1'] = ".----";
    morseList['2'] = "..---";
    morseList['3'] = "...--";
    morseList['4'] = "....-";
    morseList['5'] = ".....";
    morseList['6'] = "-....";
    morseList['7'] = "--...";
    morseList['8'] = "---..";
    morseList['9'] = "----.";
    morseList['a'] = ".-";
    morseList['b'] = "-...";
    morseList['c'] = "-.-.";
    morseList['d'] = "-..";
    morseList['e'] = ".";
    morseList['f'] = "..-.";
    morseList['g'] = "--.";
    morseList['h'] = "....";
    morseList['i'] = "..";
    morseList['j'] = ".---";
    morseList['k'] = "-.-";
    morseList['l'] = ".-..";
    morseList['m'] = "--";
    morseList['n'] = "-.";
    morseList['o'] = "---";
    morseList['p'] = ".--.";
    morseList['q'] = "--.-";
    morseList['r'] = ".-.";
    morseList['s'] = "...";
    morseList['t'] = "-";
    morseList['u'] = "..-";
    morseList['v'] = "...-";
    morseList['w'] = ".--";
    morseList['x'] = "-..-";
    morseList['y'] = "-.--";
    morseList['z'] = "--..";
}
    
void load_ascii_from_morse ()
{
    for (auto& x: morseList)
    {
        std::pair<std::string,char> pr (x.second, x.first);
        asciiList.insert (pr);
    }
}

void displaymorse()
{
    int counter = 1;
    const int width = 8;
    for (auto& x: morseList)
    {
        if (x.first != ' ')  //skip the space
        {
            std::cout << x.first << " = " << setw(width) << left << x.second;
            ++counter;
            if (counter > 6)
            {
                cout << endl;
                counter = 1;
            }
        }
    }
    cout << endl << endl;
}

void englishtomorse()
{
    std::string input = "";
    std::vector<char> notFoundItems;
    
    std::cout << "Enter a letter, number, word, or phrase to convert to morse code: ";
    std::cin.ignore();//clear any newlines cin may have left behind
    getline(std::cin, input, '\n');

    std::cout << "Desired input for translation: \"" << input << "\"\n";
    std::cout << "Morse code output: ";

    for(std::string::iterator it=input.begin(); it!=input.end(); ++it)
    {
        if (morseList.find(*it) == morseList.end())
        {
            //item was not found in the list
            //store it in a vector to report back to user
            notFoundItems.push_back(*it);
        } else {
            cout << morseList.find(*it)->second;
        }
    }

    if(notFoundItems.size() != 0)
    {
        std::cout << "\nThe following items were ignored: ";
        for(unsigned int i = 0; i<notFoundItems.size(); i++)
        {
            std::cout << "\"" << notFoundItems[i] << "\" ";
        }
    }
    std::cout << "\n\n\n";
}

void morsetoenglish()
{
    std::string input = "";
    std::vector<std::string> morse;
    std::vector<std::string> notFoundItems;

    
    std::cout << "Enter a letter, number, word, or phrase to convert to morse code: ";
    std::cin.ignore();//clear any newlines cin may have left behind
    getline(std::cin, input, '\n');

    std::cout << "User input for translation: \"" << input << "\"\n";
    std::cout << "Morse code output: ";

    morse = split(input, ' ');
    for(unsigned int i = 0; i<morse.size(); i++)
    {
        if(morse[i] == "")
        {
            std::cout << " ";
        }
        input = morse[i];

        if (asciiList.find(morse[i]) == asciiList.end())
        {
            //item was not found in the list
            //store it in a vector to report back to user
            if (morse[i] != "") {notFoundItems.push_back(morse[i]);}
        } else {
            cout << asciiList.find(morse[i])->second;
        }
    }

    if(notFoundItems.size() != 0)
    {
        std::cout << "\nThe following items were ignored: ";
        for(unsigned int i = 0; i<notFoundItems.size(); i++)
        {
            std::cout << "\"" << notFoundItems[i] << "\" ";
        }
    }

    std::cout << "\n\n\n";
}

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
{
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim))
    {
        elems.push_back(item);
    }
    return elems;
}

std::vector<std::string> split(const std::string &s, char delim)
{
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}
closed account (48T7M4Gy)
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
#include <iostream>
#include <map>
#include <string>

void display_menu();

int main()
{
    char aKey[] =
    {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
        'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
        'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
    };
    
    std::string mKey[] =
    {
        ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
        "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
        "..-","...-",".--","-..-","-.--","--..","-----",".----",
        "..---","...--","....-",".....","-....","--...","---..","----."
    };
    
    int key_sizeA = sizeof(aKey)/sizeof(char);
    
    std::map<char, std::string> alpha_to_morse;
    std::map<char, std::string>::iterator it_a_m;
    char alpha_input;
    
    std::map< std::string, char> morse_to_alpha;
    std::map< std::string, char>::iterator it_m_a;
    std::string morse_input;
    
    display_menu();
    char option = 'x';
    
    for(int i = 0; i < key_sizeA; i++)
    {
        alpha_to_morse.insert(std::pair<char, std::string>(aKey[i],mKey[i]) );
        morse_to_alpha.insert(std::pair<std::string,char>(mKey[i],aKey[i]) );
    }
    
    while ( std::cin >> option && tolower(option) != 'q')
    {
        switch(option)
        {
            case '1':
                std::cout << "Please enter alpha characters, / to end:\n";
                while( std::cin.get(alpha_input) && alpha_input != '/' )
                {
                    if(alpha_input == ' ')
                        std::cout << '|'; // vertical bar to separate words
                    
                    it_a_m = alpha_to_morse.find(alpha_input);
                    std::cout << it_a_m -> second << ' ';
                }
                break;
                
            case '2':
                std::cout << "Please enter morse string:\n";
                while( std::cin >> morse_input && morse_input != "/" )
                {
                    if(morse_input == "|")
                        std::cout << " ";
                    else
                    {
                        it_m_a = morse_to_alpha.find(morse_input);
                        std::cout << it_m_a -> second;
                    }
                }
                break;
                
            default:
                std::cout << "Invalid choice\n";
        }
        display_menu();
    }
    
    return 0;
}

void display_menu()
{
    std::cout << "*********************\n";
    std::cout << "* 1. alpha -> morse *\n";
    std::cout << "* 2. morse -> alpha *\n";
    std::cout << "* q  Quit program   *\n";
    std::cout << "*********************\n";
}
Topic archived. No new replies allowed.
Pages: 12