Converting english to morse code

Pages: 12
The fundamental problem here is that when you say morse[ tolower(aCharacter) ] you're assuming that the ASCII code for the digits 0-9 follows right after the code for letters 'a'-'z'. In fact the ASCII code for 'z' is 122 and the code for 0 is 48. So the digits actually come before the letters, way before them.

Beyond that my three most important bits of advice are:
1. keep it simple.
2. KEEP IT SIMPLE.
3. KEEP. IT. SIMPLE.

You don't need a map. It's completely inappropriate for this probem. You're mapping contiguous integral values to fixed strings. The mapping never changes. That calls for an array. You don't even need it to be an array of strings (because the values don't change). Save the space and use const char* instead. On my computer that simple change reduces the size of the program by 20%

Okay, so you need 2 arrays: one for the letters and one for the digits:
1
2
3
4
5
6
7
8
9
10
const char * morseLetters[26] =
{
    ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
    "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
    "..-","...-",".--","-..-","-.--","--.."
};
const char* morseDigits[10] = {
    "-----", ".----", "..---","...--","....-", // 0-4
    ".....", "-....","--...","---..","----."   // 5-9
};


Then toMorse() needs to figure out which one to use. I'm assuming that if the character isn't a letter or digit then we should return the character unchanged:
1
2
3
4
5
6
7
8
9
10
string toMorse (char aCharacter)
{
    if (isalpha(aCharacter)) {
        return  morseLetters[ tolower(aCharacter) - 'a'];
    } else if (isdigit(aCharacter)) {
        return  morseDigits[ aCharacter - '0'];
    } else {
        return string(1, aCharacter);
    }
}



Have you learned about static variables? Since the arrays are only used by toMorse, they would ideally be static inside that function. Then you can simplify their names to just letters and digits.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string toMorse (char aCharacter)
{
    static const char * letters[26] =
        {
            ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
            "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
            "..-","...-",".--","-..-","-.--","--.."
        };
    static const char* digits[10] = {
        "-----", ".----", "..---","...--","....-", // 0-4
        ".....", "-....","--...","---..","----."   // 5-9
    };

    if (isalpha(aCharacter)) {
        return  letters[ tolower(aCharacter) - 'a'];
    } else if (isdigit(aCharacter)) {
        return  digits[ aCharacter - '0'];
    } else {
        return string(1, aCharacter);
    }
}
Wait then why didn't it work for me that's weird if that was all that was wrong with it then i wouldn't have even noticed but the program literally just crashed maybe its my pc.
It crashed because you were accessing the array out of bounds.
Hello, @TheKingGamer117. Just as a matter of interest, what characters do the first three (out of 39 = 3 + 10 + 26) of your strings represent?
"--..--", ".-.-.-", "..--.."
Are these contiguous with digits or letters in the ASCII character set?

Also, could you indicate what was your code and what phrases you were trying to input when your computer crashed.
@Lastchance oh sorry for the late reply school then work..... but the first three are a comma a period and a question mark in that order im usiing the book starting out with c++ from control structures and on page 591 (if you have it) has a mores code chart.

space space 6 -.... G --. Q --.
comma --..-- 7 --... H .... R .-.
period .-.-.- 8 ---.. I .. S ...
question mark ..--.. 9 ----. J .--- T -
0 ----- A .- K -.- U ..-
1 .---- B -... L .-.. V ...-
2 ..--- C -.-. M -- W .--
3 ...-- D -.. N -. X -..
4 ....- E . O --- Y -.-
5 . ... F ..-. P .--. Z --..

kinda looks something like that why???

also the code i was trying to use was the original from the first post then this one
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
#include <iostream>
#include <string>

using namespace std;

const int morse_size = 39;
const string morse[morse_size] =
{
    ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
    "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
    "..-","...-",".--","-..-","-.--","--..","-----",".----",
    "..---","...--","....-",".....","-....","--...","---..","----."
};
string toMorse (char);

int main ()
{
    
    string text, coded_text;
    
    cout << "Please enter a character, word or phrase to translate from english to morse code \n";
    
    getline(cin,text);
    cout << "Your text \n" << text << endl;
    
    for(int i = 0; i < text.length(); i++ )
    {
        coded_text += toMorse(text[i]) + ' ';
    }
    
    cout << "Here it is in morse code \n " << coded_text << endl;
    
    return 0;
}


string toMorse (char aCharacter)
{
    return  morse[ tolower(aCharacter) - 'a'];;
}


but in both when i entered any number lets just say 9 it would just crash
hope this helps
Why did you not use std::map? std::map and [std::unordered_map are immensely useful.
You have to use parallel arrays if you wish to solve the problem.

morse[ tolower(aCharacter) - 'a'];

This is extremely deadly because your array can go out of bounds at anytime.

1
2
3
4
    ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
    "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
    "..-","...-",".--","-..-","-.--","--..","-----",".----",
    "..---","...--","....-",".....","-....","--...","---..","----."


Personally I think this part is a mess and you are not self-documenting what these groups of symbols mean.
closed account (48T7M4Gy)
@OP By modifying the original code I posted so that it prints out the index instead of the actual code corresponding (or "mapping" (not "<map>ping" ) to that index you can see why my blooper, leads to out of range behaviour. Some of the index values are negative which is double-bad because array index number can only be >=0 as well as being in range.

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

using namespace std;

const int morse_size = 39;
const string morse[morse_size] =
{
    ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
    "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
    "..-","...-",".--","-..-","-.--","--..","-----",".----",
    "..---","...--","....-",".....","-....","--...","---..","----."
};
int toMorse (char);

int main ()
{
    
    string text, coded_text;
    
    cout << "Please enter a character, word or phrase to translate from english to morse code \n";
    
    getline(cin,text);
    cout << "Your text \n" << text << endl;
    
    for(int i = 0; i < text.length(); i++ )
    {
        cout << toMorse(text[i]) << ' '; // <---
    }
    
    //cout << "Here it is in morse code \n " << coded_text << endl;
    
    return 0;
}


int toMorse (char aCharacter)
{
    return  (int)(aCharacter - 'a'); // <---
}


Please enter a character, word or phrase to translate from english to morse code 
abcxyz 01289
Your text 
abcxyz 01289
0 1 2 23 24 25 -65 -49 -48 -47 -41 -40 Program ended with exit code: 0
I don't understand that why do we need to use arrays knowing that they are problematic at least for beginners? We can use a switch statement to solve the problem instead.

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

using namespace std;

string toMorse (char);

int main ()
{
    string text, coded_text;
    
    cout << "Please enter a character, word or phrase to translate from english to morse code \n";
    
    getline(cin,text);
    cout << "Your text \n" << text << endl;
    
    for(int i = 0; i < text.length(); i++ )
    {
        coded_text += toMorse(text[i]) + ' ';
    }
    
    cout << "Here it is in morse code \n " << coded_text << endl;
    
    return 0;
}

string toMorse (char aCharacter)
{
	string morse;
	switch(toupper(aCharacter))
	{
		case 'A' : morse = ".-"; break;
		case 'B' : morse = "-..."; break;
		case 'C' : morse = "-.-."; break;
		case 'D' : morse = "-.."; break;
		case 'E' : morse = "."; break;
		case 'F' : morse = "..-."; break;
		case 'K' : morse = "-.-"; break;
		case 'L' : morse = ".-.."; break;
		case 'M' : morse = "--"; break;
		case 'N' : morse = "-."; break;
		case 'O' : morse = "---"; break;
		case 'P' : morse = ".--."; break;
		case 'U' : morse = "..-"; break;
		case 'V' : morse = "...-"; break;
		case 'W' : morse = ".--"; break;
		case 'X' : morse = "-.."; break;
		case 'Y' : morse = "-.-"; break;
		case 'Z' : morse = "--."; break;
		case 'G' : morse = "--."; break;
		case 'H' : morse = "...."; break;
		case 'I' : morse = ".."; break;
		case 'J' : morse = ".---"; break;
		case 'Q' : morse = "--."; break;
		case 'R' : morse = ".-."; break;
		case 'S' : morse = "..."; break;
		case 'T' : morse = "-"; break;

		case ' ' : morse = "space"; break;
		case '0' : morse = "-----"; break;
		case '1' : morse = ".----"; break;
		case '2' : morse = "..---"; break;
		case '3' : morse = "...--"; break;
		case '4' : morse = "....-"; break;
		case '5' : morse = ". ..."; break;
		case '6' : morse = "-...."; break;
		case '7' : morse = "--..."; break;
		case '8' : morse = "---.."; break;
		case '9' : morse = "----."; break;

		case ',' : morse = "--..--"; break;
		case '.' : morse = ".-.-.-"; break;
		case '?' : morse = "..--.."; break;

		default : morse = "Unknown";
	};

    return morse;
}


Please enter a character, word or phrase to translate from english to morse code

Good day.

Your text
Good day.
Here it is in morse code
 --. --- --- -.. space -.. .- -.- .-.-.-
Press any key to continue


@OP : I created the switch statement based on the morse code table you have. I don't have time to verify the information correctness though.
space space 6 -.... G --. Q --.
comma --..-- 7 --... H .... R .-.
period .-.-.- 8 ---.. I .. S ...
question mark ..--.. 9 ----. J .--- T -
0 ----- A .- K -.- U ..-
1 .---- B -... L .-.. V ...-
2 ..--- C -.-. M -- W .--
3 ...-- D -.. N -. X -..
4 ....- E . O --- Y -.-
5 . ... F ..-. P .--. Z --..

why do we need to use arrays knowing that they are problematic at least for beginners?
Because using arrays is one of the first things that beginners should learn to do. Also, it's very valuable lesson to learn to use data instead of code to solve a problem.
Topic archived. No new replies allowed.
Pages: 12