Converting text into morse code

What's up guys. My assignment is to convert user inputted text into the designated morse code value for each letter in the alphabet. I can't figure out how to read into my user's input and convert each given letter into the represented morse code value. Here is my code so far:

#include <iostream>
#include <string>

int main() {

std::string morseCode[26];
morseCode[0] = ".-"; //A
morseCode[1] = "-..."; //B
morseCode[2] = "-.-."; //C
morseCode[3] = "-.."; //D
morseCode[4] = "."; //E
morseCode[5] = "..-."; //F
morseCode[6] = "--."; //G
morseCode[7] = "...."; //H
morseCode[8] = ".."; //I
morseCode[9] = ".---"; //J
morseCode[10] = "-.-"; //K
morseCode[11] = ".-.."; //L
morseCode[12] = "--"; //M
morseCode[13] = "-."; //N
morseCode[14] = "---"; //O
morseCode[15] = ".--."; //P
morseCode[16] = "--.-"; //Q
morseCode[17] = ".-."; //R
morseCode[18] = "..."; //S
morseCode[19] = "-"; //T
morseCode[20] = "..-"; //U
morseCode[21] = "...-"; //V
morseCode[22] = ".--"; //W
morseCode[23] = "-..-"; //X
morseCode[24] = "-.--"; //Y
morseCode[25] = "--.."; //Z

std::string userInput;
std::cout << "Input a message to translate into Morse code:" <<std::endl;
std::getline(std::cin, userInput);

std::string translate = "";

for(int i = 0; i < userInput.length(); i++) {

translate += morseCode[i];
std::cout << translate;

}


return 0;
}
this is a good start !

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

int main() {

std::string morseCode[26];
morseCode[0] = ".-"; //A
morseCode[1] = "-..."; //B
morseCode[2] = "-.-."; //C
morseCode[3] = "-.."; //D
morseCode[4] = "."; //E
morseCode[5] = "..-."; //F
morseCode[6] = "--."; //G
morseCode[7] = "...."; //H
morseCode[8] = ".."; //I
morseCode[9] = ".---"; //J
morseCode[10] = "-.-"; //K
morseCode[11] = ".-.."; //L
morseCode[12] = "--"; //M
morseCode[13] = "-."; //N
morseCode[14] = "---"; //O
morseCode[15] = ".--."; //P
morseCode[16] = "--.-"; //Q
morseCode[17] = ".-."; //R
morseCode[18] = "..."; //S
morseCode[19] = "-"; //T
morseCode[20] = "..-"; //U
morseCode[21] = "...-"; //V
morseCode[22] = ".--"; //W
morseCode[23] = "-..-"; //X
morseCode[24] = "-.--"; //Y
morseCode[25] = "--.."; //Z

std::string userInput;
std::cout << "Input a message to translate into Morse code:" <<std::endl;
std::getline(std::cin, userInput);

std::string translate = "";

for(int i = 0; i < userInput.length(); i++) 
{  
      int c = userInput[i] - 97;
      if (c >= 0 && c <=25) 
           translate += morseCode[c];
}
std::cout << translate;

return 0;
}



I pulled out the cout << of the loop to get the result only once.

Now most important is that the index of the code is not the position of the letter in the userInput, but its value (taking into account this is an ASCII code).

You can easily change the code to make it no case sensitive.
Thank you very much for your response.

Could you please explain why you are subtracting userInput[i] by 97, then checking to see if c >= 0 and <= 25?
@youngldoe

Lower case 'a' is 97 in decimal, so, subtracting 97 assigns the variable c, a 0. morseCode[0] is morse for the letter 'a'. A couple of small problems I see though. If the user types in upper and lower case, the result will be a mess. So, you a check should be done to see if the variable first is between 65 and 90. 65 is upper case 'A' and 90 is upper case 'Z'. If it is, then subtract only 65, to get the same results as with lower case. Another problem is, if the message has a space in it, as most messages will. A word shouldn't, but a message, most definitely. And, what about numbers?. Okay, you have a good start though.
Last edited on
closed account (48T7M4Gy)
https://en.wikipedia.org/wiki/Morse_code shows you what to do for additional characters.

Morse code only concerns itself with upper case which makes life easy.
@kemort

Morse code only concerns itself with upper case which makes life easy.


But, if the user were to type in a lower case letter, it would mess up the program. So,
line 41 above, should be type in as int c = toupper(userInput[i]) - 97;
closed account (48T7M4Gy)
And spaces have a particular coding.
Topic archived. No new replies allowed.