Text to Morse Code

So I don't actually have any way to test this because this is supposed to be part of a larger program that I'm working on. The send_high and send_low functions will send either a high or low signal to an LED that will blink the morse code version of a certain message out. Right now I just have the message hardcoded into the program, but the message will actually be determined based on a prompt from a separate remote. Anyways, I was just wondering if anyone could help me out here and tell me if this code looks right. I'm passing around a lot of arrays and I'm not quite exactly comfortable with doing that, so I'm not sure if I'm doing it right or not. One of my biggest concerns is the array of strings that holds the dits and dahs for the morse code, because it's an array of an array of characters so I'm not exactly sure how to deal with passing that in and out of functions. I know that I should have the functions declared at the top, but like I said this is supposed to be part of a bigger program. Can I return the array of characters like I'm doing? My thought process for this program was that I take a message, I pass it into the message_to_morse function, that function then breaks it down into the individual characters and passes those characters into the character_to_morse function, which finds the character in the alphabet array. The location of the character in the alphabet array corresponds to the location of it's morse counterpart in the morse array, so I then return an array that holds the dits and dah's of the morse counterpart of the character to the message_to_morse array, and then I return that to the main array, where depending on the contents of the array(whether it's a dit or a dah or a space), it sends high's and lows to the LED so that the blinking corresponds to the morse-code of the message. Does that make sense? Am I doing it right? I feel like the message_to_morse function isn't actually necessary and I could just do that in the main function. Any help would be greatly appreciated. Thank you very much.

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
char alphabet[] = "abcdefghijklmnopqrstuvwxyz/|}" // '/' means space between letters, '|' means space between words, '}' means end of message
string morse[] {".-","-...","-.-.","-..", ".", "..-.", "--.", "....", "..", 
	".---", "-.-", ".-..", "--","-.", "---", ".--.", "--.-", ".-.", "...", "-",
	"..-", "...-", ".--", "-..-", "-.--", "--..","   ","       ",".-.-."};

void main(){
	
	
	char message[] = ('whatever');
	char morse_code[] = message_to_morse(message);
	int i = 0;
	while (morse_code[i] != NULL){
		if(morse_code[i] == '.'){
			send_high();
			send_low();
		}
		else if(morse_code[i] == '-'){
			send_high();
			send_high();
			send_high();
			send_low();
		}
		else if(morse[i]_code == ' '){
			send_low();
		}
		i++;
	}	
}


char message_to_morse[](char  c[]){
	
	int i = 0;
	char morse_message[];
	while(c[i] != NULL){
		morse_message[i] = character_to_morse(c[i]);
	}
	return morse_message;
}
char character_to_morse[](char c){
	char found[];
	int index = 0;
	while(true){
		if(alphabet[index] == c){
			found = morse[index];
			break;
		}else{
			index++;
		}
	}
	
	return found;
}


	


Last edited on
This looks pretty good, but you're doing an awful lot of work...

Parallel arrays can be dangerous because if you want to change one array, you really need to remember to change both.

Introducing MAP! You could do the same thing with this:
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
#include <map>
#include <string>
#include <iostream>

class Morse
{
  typedef std::pair<char,std::string> set;
  std::map<char,std::string> themap;

  std::string Char2Morse(const char c)
  {
    return themap[c];
  }

public:
  Morse()
  {
    themap['a'] = ".-";
    themap['b'] = "-...";
    themap['c'] = "-.-.";
    //...
    themap['/'] = "       ";
    themap['|'] = ".-.-.";
  }

  std::string Str2Morse(const std::string s)
  {
    std::string output;
    for (int i = 0; i < s.length(); ++i)
    {
      output += Char2Morse( s[i] );
    }
    return output;
  }
};
static Morse morse;

int main()
{
  std::string message = "a/b/c/|";
  std::string morse_code = morse.Str2Morse(message);

  std::cout << message << std::endl;
  std::cout << " = " << std::endl;
  std::cout << morse_code << std::endl;
}
a/b/c/|
 = 
.-       -...       -.-.       .-.-.

Last edited on
Thank you so much! Your way is much easier haha. I didn't even know you could do maps in C++ I thought that was a Java thing. Thank you again!
Topic archived. No new replies allowed.