Morse Code Translator

Hey,
I'm working on an English to Morse Code translator and have encountered a problem I haven't been able to beat for awhile. The problem is when I run the code (or program, I don't know the nomenclature just yet) and type in my input it simply returns nothing. I'm completely at a loss, and am sorry to probably be posting a naive question. My code is shown below.

The output is as follows...
"Type in message. Numbers and lowercase letters only, please.
morse code
In Morse Code, your message is..."

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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

const char* MorseAlphabet[] = {". - ", "- . . . ", "- . - .", "- . . ", ". ",
        ". . - .", " - - . ", ". . . . ", ". . ", ". - - - ", "- . - ", ". - . . ", "- - ", "- . ",
        "- - - ", ". - - . ", "- - . - ", ". - . ", ". . . ", "- ",
        ". . - ", ". . . - ", ". - - ", "- . . - ", "- . - - ",
        "- - . . ", "  "};

const char* MorseNumbers[] = {". - - - - ", ". . - - - ", ". . . - - ", ". . . . - ",
        ". . . . . ", "- . . . . ", "- - . . . ", "- - - . . ", "- - - - . ", "- - - - - "};

char EnglishAlphabet[] = "abcdefghijklmnopqrstuvwxyz ";

char EnglishNumbers[] = "0123456789";

int main()
{
    string Input;
    int i = 0;
    int k = 0;


    cout << "Type in message. Numbers and lowercase letters only, please.\n";
    getline (cin, Input);
    stringstream(Input) >> i;

    for(unsigned x = 0; x < Input.length(); ++x){
    Input.at(x) = k;
    }

    if(EnglishAlphabet[k] == i){
        cout << MorseAlphabet[k];
    }
    else if(EnglishNumbers[k] == i){
        cout << MorseNumbers[k];
    }
    cout << "In Morse Code, your message is...\n";
}
Last edited on
Why are you mixing c-strings and strings?

Line 28 gets only the first character that you try to use for lines 34-39.
Lines 34-39 are outside of the loop so k == Input.length() - 1.

I would personally use a map for this but that's just me. To do it your way you are going to have to see if they are a letter or number and then act accordingly.
C-strings and strings? I literally just started teaching myself c++ a few days ago, and don't know what the difference between c-strings and strings. A map? I'll look into that. For the meantime, however, can someone put me on the right track for determining if the input is a number of letter?

Here is my tweaked code; the problem remains the same.
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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

const char* MorseArray[] = {".- ", "-... ", "-.-. ", "-.. ", ". ",
        "..-.", "--. ", ".... ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ",
        "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ",
        "..- ", "...- ", ".-- ", "-..- ", "-.-- ",
        "--.. ", "  ", ".---- ", "..--- ", "...-- ", "....- ",
        "..... ", "-.... ", "--... ", "---.. ", "----. ", "----- "};

char EnglishArray[] = "abcdefghijklmnopqrstuvwxyz 0123456789";

int main()
{
    string Input;
    int k = 0;

    cout << "Type in message. Numbers and lowercase letters only, please.\n";
    getline (cin, Input);
    stringstream(Input) >> k;

    cout << "In Morse Code, your message is... \n";

    for(k < Input.length(); ++k;){

        if(EnglishArray[k] == Input[k])
        cout << MorseArray[k];
        return k;
    }
}
Instead of just typing code, map out what you want to do first. I think you'll find it more productive.

For each character in Input find the index for the corresponding element in EnglishArray. If there is such an index, you may safely use the it to select the translation from MorseArray.
You mean something like this? Now it only works if I input "a", and gives the blank response for any other input.

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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

const char* MorseArray[] = {".- ", "-... ", "-.-. ", "-.. ", ". ",
        "..-.", "--. ", ".... ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ",
        "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ",
        "..- ", "...- ", ".-- ", "-..- ", "-.-- ",
        "--.. ", "  ", ".---- ", "..--- ", "...-- ", "....- ",
        "..... ", "-.... ", "--... ", "---.. ", "----. ", "----- "};

char EnglishArray[] = "abcdefghijklmnopqrstuvwxyz 1234567890";

int main()
{
    string Input;
    unsigned k = 0;

    cout << "Type in message. Numbers and lowercase letters only, please.\n";
    getline (cin, Input);
    stringstream(Input) >> k;

    cout << "In Morse Code, your message is... \n";

    for(k < Input.length(); ++k;){

        if(EnglishArray[0] == Input[0])
            cout << MorseArray[0];

        else if(EnglishArray[1] == Input[1])
            cout << MorseArray[1];

        else if(EnglishArray[2] == Input[2])
            cout << MorseArray[2];

        else if(EnglishArray[3] == Input[3])
            cout << MorseArray[3];

        else if(EnglishArray[3] == Input[3])
            cout << MorseArray[3];

        else if(EnglishArray[4] == Input[4])
            cout << MorseArray[4];

        else if(EnglishArray[5] == Input[5])
            cout << MorseArray[5];

        else if(EnglishArray[6] == Input[6])
            cout << MorseArray[6];

        else if(EnglishArray[7] == Input[7])
            cout << MorseArray[7];

        else if(EnglishArray[8] == Input[8])
            cout << MorseArray[8];

        else if(EnglishArray[9] == Input[9])
            cout << MorseArray[9];

        else if(EnglishArray[10] == Input[10])
            cout << MorseArray[10];

        else if(EnglishArray[11] == Input[11])
            cout << MorseArray[11];

        else if(EnglishArray[12] == Input[12])
            cout << MorseArray[12];

        else if(EnglishArray[13] == Input[13])
            cout << MorseArray[13];

        else if(EnglishArray[14] == Input[14])
            cout << MorseArray[14];

        else if(EnglishArray[15] == Input[15])
            cout << MorseArray[15];

        else if(EnglishArray[15] == Input[15])
            cout << MorseArray[15];

        else if(EnglishArray[16] == Input[16])
            cout << MorseArray[16];

        else if(EnglishArray[17] == Input[17])
            cout << MorseArray[17];

        else if(EnglishArray[18] == Input[18])
            cout << MorseArray[18];

        else if(EnglishArray[19] == Input[19])
            cout << MorseArray[19];

        else if(EnglishArray[20] == Input[20])
            cout << MorseArray[20];

        else if(EnglishArray[21] == Input[21])
            cout << MorseArray[21];

        else if(EnglishArray[22] == Input[22])
            cout << MorseArray[22];

        else if(EnglishArray[23] == Input[23])
            cout << MorseArray[23];

        else if(EnglishArray[24] == Input[24])
            cout << MorseArray[24];

        else if(EnglishArray[25] == Input[25])
            cout << MorseArray[25];

        else if(EnglishArray[26] == Input[26])
            cout << MorseArray[26];

        else if(EnglishArray[27] == Input[27])
            cout << MorseArray[27];

        else if(EnglishArray[28] == Input[28])
            cout << MorseArray[28];

        else if(EnglishArray[29] == Input[29])
            cout << MorseArray[29];

        else if(EnglishArray[30] == Input[30])
            cout << MorseArray[30];

        else if(EnglishArray[31] == Input[31])
            cout << MorseArray[31];

        else if(EnglishArray[32] == Input[32])
            cout << MorseArray[32];

        else if(EnglishArray[33] == Input[33])
            cout << MorseArray[33];

        else if(EnglishArray[34] == Input[34])
            cout << MorseArray[34];

        else if(EnglishArray[35] == Input[35])
            cout << MorseArray[35];

        else if(EnglishArray[36] == Input[36])
            cout << MorseArray[36];

        return k;
    }
}
@senuba91

No, not like that. You need 2 for loops. The outer loop gets each letter in the sentence, one at a time, while the second loop, checks each of the values in MorseArray[], to see if it matches the outer array. Like so..
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
#include <iostream>
#include <cstring>
#include <sstream>

using namespace std;
// Changed below to a string
const string MorseArray[] = {".- ", "-... ", "-.-. ", "-.. ", ". ",
 "..-.", "--. ", ".... ", ".. ", ".--- ", "-.- ", ".-.. ", "-- ", "-. ",
 "--- ", ".--. ", "--.- ", ".-. ", "... ", "- ",
 "..- ", "...- ", ".-- ", "-..- ", "-.-- ",
 "--.. ", "  ", ".---- ", "..--- ", "...-- ", "....- ",
 "..... ", "-.... ", "--... ", "---.. ", "----. ", "----- "};

string EnglishArray = "abcdefghijklmnopqrstuvwxyz 0123456789"; // Changed to a string

int main()
{
 string Input;
 int sentence,len;
 cout << "Type in message." << endl << "( Capital letters will be converted to lowercase )" << endl;
 getline (cin, Input);

 //stringstream(Input) >> k;

 cout << "In Morse Code, your message is..." << endl;

sentence = Input.length();
len = EnglishArray.length();

 for(int k=0; k < sentence; k++)
for(int x = 0;x < len;x++)
 	if(EnglishArray[x] == tolower(Input[k])) // Check letter as a lower case
	 cout << MorseArray[x];

cout << endl << endl;
 }
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
if(EnglishArray[0] == Input[0])
            cout << MorseArray[0];

        else if(EnglishArray[1] == Input[1])
            cout << MorseArray[1];

        else if(EnglishArray[2] == Input[2])
            cout << MorseArray[2];

        else if(EnglishArray[3] == Input[3])
            cout << MorseArray[3];

        else if(EnglishArray[3] == Input[3])
            cout << MorseArray[3];

        else if(EnglishArray[4] == Input[4])
            cout << MorseArray[4];

        else if(EnglishArray[5] == Input[5])
            cout << MorseArray[5];

        else if(EnglishArray[6] == Input[6])
            cout << MorseArray[6];

        else if(EnglishArray[7] == Input[7])
            cout << MorseArray[7];

        else if(EnglishArray[8] == Input[8])
            cout << MorseArray[8];

        else if(EnglishArray[9] == Input[9])
            cout << MorseArray[9];

        else if(EnglishArray[10] == Input[10])
            cout << MorseArray[10];

        else if(EnglishArray[11] == Input[11])
            cout << MorseArray[11];

        else if(EnglishArray[12] == Input[12])
            cout << MorseArray[12];

        else if(EnglishArray[13] == Input[13])
            cout << MorseArray[13];

        else if(EnglishArray[14] == Input[14])
            cout << MorseArray[14];

        else if(EnglishArray[15] == Input[15])
            cout << MorseArray[15];

        else if(EnglishArray[15] == Input[15])
            cout << MorseArray[15];

        else if(EnglishArray[16] == Input[16])
            cout << MorseArray[16];

        else if(EnglishArray[17] == Input[17])
            cout << MorseArray[17];

        else if(EnglishArray[18] == Input[18])
            cout << MorseArray[18];

        else if(EnglishArray[19] == Input[19])
            cout << MorseArray[19];

        else if(EnglishArray[20] == Input[20])
            cout << MorseArray[20];

        else if(EnglishArray[21] == Input[21])
            cout << MorseArray[21];

        else if(EnglishArray[22] == Input[22])
            cout << MorseArray[22];

        else if(EnglishArray[23] == Input[23])
            cout << MorseArray[23];

        else if(EnglishArray[24] == Input[24])
            cout << MorseArray[24];

        else if(EnglishArray[25] == Input[25])
            cout << MorseArray[25];

        else if(EnglishArray[26] == Input[26])
            cout << MorseArray[26];

        else if(EnglishArray[27] == Input[27])
            cout << MorseArray[27];

        else if(EnglishArray[28] == Input[28])
            cout << MorseArray[28];

        else if(EnglishArray[29] == Input[29])
            cout << MorseArray[29];

        else if(EnglishArray[30] == Input[30])
            cout << MorseArray[30];

        else if(EnglishArray[31] == Input[31])
            cout << MorseArray[31];

        else if(EnglishArray[32] == Input[32])
            cout << MorseArray[32];

        else if(EnglishArray[33] == Input[33])
            cout << MorseArray[33];

        else if(EnglishArray[34] == Input[34])
            cout << MorseArray[34];

        else if(EnglishArray[35] == Input[35])
            cout << MorseArray[35];

        else if(EnglishArray[36] == Input[36])
            cout << MorseArray[36];
One word: LOOP

Line 145 return k; why is this here??

By map I meant to use a std::map<T, U> T = key, U = value

So something like:

std::map<char, string> codes = { {'a', ".-"}, {'b', "-..."} }; //with all ofcourse

Then to access you would simply use the [] operator, .at(), or .find()
http://www.cplusplus.com/reference/map/map/


Though to do it the way you are. You will want to iterate over the string that is being converted to morse code. If it is a letter output (or append to morse string) MorseArray[alphanum - 'a']; and if it is a number output(or append to string) MorseArray[26 +alphanum - '0'];

There are several ways to tell if it is a number or letter. The easiest would b e isalpha(character) and isnum(character) there is also if(character >= 'a' && character <= 'z') //lower case letter(I would suggest making them all lower or upper and if(character >= '0' && character <= '9') //is number then, there is your method where you make a list of letters/characters which is probably the least effective.

I tried it with the changes that whitenite1 made and it works like a charm. I'll try to recreate the program using maps for extra practice. Thank you so much everyone, your help is much appreciated.
Topic archived. No new replies allowed.