converting morse code to english

Hey guys, i am new to programming and trying to write a program that converts english to morse and vice versa.

i wrote the english to morse part but i am struggling with morse to english,
it has to be a separate function that accepts the input message, alphanumeric array, and the morse array and returns the converted message.



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

string englishToMorse(const string englishText, const char alphaN[], const string morse[], int size)
{
	string morseText = " ";
	for (int i = 0; i < englishText.length(); i++) {
		for (int n = 0; n < size; n++) {
			if (englishText[i] == alphaN[n]) {
				morseText += morse[n] + ' ';
			}

		}
	}
	
	return morseText;
}



string morseToEnglish(const string morseText, const char alphaN[], const string morse[], int size)
{
	string convText;


	
}




int main()
{
	string englishText, morseText, convMorse, convText;
	const int SIZE = 39;
	string morse[SIZE] =
	{
		".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
		"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
		"..-","...-",".--","-..-","-.--","--..","-----",".----",
		"..---","...--","....-",".....","-....","--...","---..","----."
	};

	char alphaN[SIZE] =
	{
		'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'
	};
	int choice = 0;


	while (choice != 3) {
		cout << "Please choose an option <1/2/3>" << endl;
		cout << "1. Convert English text message to Morse Code." << endl;
		cout << "2. Convert Morse Code to English text message." << endl;
		cout << "3. Exit." << endl << endl;

		cout << "Enter your choice: ";
		cin >> choice;

		switch (choice) {

			case 1:
				cout << "Please type in your message in English" << endl;
				cin.ignore();
				getline(cin, englishText);
				


				cout << "you typed " << englishText << endl;

				convMorse = englishToMorse(englishText, alphaN, morse, SIZE);

				cout << convMorse << endl << endl;
				break;

		

			case 2: 
			
			break;

			case 3:
			cout << "thanks for using the program.";

		}
	}

	return 0;
}


Thanks!!
Last edited on
what has you stuck?

which you can note I made them put a space between blocks of morse. You need a way to tell when to stop reading because the lengths vary for the letters.
For your code, its just the reverse lookup table... you get "..." what should pop out?
... actually I see you didn't use a lookup table, but a linear search instead. regardless, its the same idea, just reverse the input and output.

morse code makes a nifty tree structure... from an old post.. much less clutter/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
struct mctx //lets make a little struct for this exercise
{
  char c;
  unsigned char dot;
  unsigned char dash;	
};

int main()
{
int dx = 0;
mctx mct[50]; //a table of our structs.  I left room for the numbers later. 
char init[] = "\0etianmsurwdkgohvf\0l\0pjbxcyzq"; //there is madness here. do you see it?
//hint, look at etianm as dots and dashes.  then it will leap out. 
//lets load up a table of our struct. 
for(dx = 0; dx < 30; dx++)
{
  mct[dx].c = init[dx];	 //assuming you got the pattern above, then 
  mct[dx].dot = (dx*2)+1;   //what is this doing?  do you see? 
  mct[dx].dash = (dx*2)+2;
}

//a classic example code
char word[] = "... --- ... --- "; //cheesy approach needs extra space or stx on end.

//and now we just turn the code back to text by tracing the paths. 
for(int i=0, dx = 0; dx < strlen(word); dx++)
{      
    switch(word[dx])
	{
    case '.': 
	i = mct[i].dot;
	break;
	case '-': 
	i = mct[i].dash;
	break;
	case ' ': 
	cout << mct[i].c;
	i = 0;
	break;  	
	}	
}

}
Last edited on
the problem is the spaces

if the input is ... --- ...

how do i make it so it takes the first part until a space (...) and compare it to the morse array, then move to the next part (---) and do the same thing, and so on.
a mix of getline, find, and substring
or
stream operators (<< ) ignore whitespace and use it as a delimiter and it will break up naturally.

or as I did, reset your conversion when you see a space in a string. Mine works on space delimited strings so getline would just work with that approach without the find and substring

eg
1
2
3
4
5
6
7
8
9
10
11
12
string tmp;
for(all the letters in the input getline)
{
    if letter is not a space, 
    tmp+= letter;
    else   //line 36 in mine
    {
      look up tmp and convert to letter
       print letter or save it in a result string or something.
       tmp = "";
    }
}
Last edited on
Based on original method, perhaps simply:

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
#include <iostream>
#include <iomanip>
#include <string>
#include <iterator>
#include <sstream>
#include <cctype>

using namespace std;

string englishToMorse(const string& englishText, const char alphaN[], const string morse[], size_t size)
{
	string morseText;

	for (size_t i = 0; i < englishText.length(); ++i)
		for (size_t n = 0; n < size; ++n)
			if (static_cast<char>(std::toupper(static_cast<unsigned char>(englishText[i]))) == alphaN[n])
				morseText += morse[n] + ' ';

	return morseText;
}

string morseToEnglish(const string& morseText, const char alphaN[], const string morse[], size_t size)
{
	string convText;
	std::istringstream iss(morseText);

	for (std::string m; iss >> m;)
		for (size_t n = 0; n < size; ++n)
			if (morse[n] == m)
				convText += alphaN[n];

	return convText;
}

int main()
{
	const string morse[]
	{
		".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
		"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
		"..-","...-",".--","-..-","-.--","--..","-----",".----",
		"..---","...--","....-",".....","-....","--...","---..","----."
	};

	const char alphaN[]
	{
		'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'
	};

	static_assert(std::size(morse) == std::size(alphaN));

	int choice {};

	while (choice != 3) {
		cout << "Please choose an option <1/2/3>\n";
		cout << "1. Convert English text message to Morse Code.\n";
		cout << "2. Convert Morse Code to English text message.\n";
		cout << "3. Exit.\n\n";

		cout << "Enter your choice: ";
		cin >> choice;

		switch (choice) {
			case 1:
			{
				string englishText;

				cout << "Please type in your message in English\n";
				getline(cin >> ws, englishText);
				//cout << "you typed " << englishText << '\n';

				cout << englishToMorse(englishText, alphaN, morse, std::size(alphaN)) << "\n\n";
			}
				break;

			case 2:
			{
				string morseText;

				cout << "Please type in your message in morse\n";
				getline(cin >> ws, morseText);
				//cout << "you typed " << morseText << '\n';

				cout << morseToEnglish(morseText, alphaN, morse, std::size(alphaN)) << "\n\n";
			}
				break;

			case 3:
				cout << "thanks for using the program.\n";
				break;

			default:
				cout << "Invalid option\n";
		}
	}
}



Please choose an option <1/2/3>
1. Convert English text message to Morse Code.
2. Convert Morse Code to English text message.
3. Exit.

Enter your choice: 1
Please type in your message in English
seeplus
... . . .--. .-.. ..- ...

Please choose an option <1/2/3>
1. Convert English text message to Morse Code.
2. Convert Morse Code to English text message.
3. Exit.

Enter your choice: 2
Please type in your message in morse
... . . .--. .-.. ..- ...
SEEPLUS

Please choose an option <1/2/3>
1. Convert English text message to Morse Code.
2. Convert Morse Code to English text message.
3. Exit.

Enter your choice: 3
thanks for using the program.


The issue is the separation of morse into words. In practice this is done by time difference between pauses between letters/words.
Thank you so much!!
I know this is not the most efficient way but i am still learning the basics :). i decided to copy the contents of tmp into an array, increase the subscript and reset tmp.

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
string morseToEnglish(const string morseText, const char alphaN[], const string morse[], int size)
{
	string convText[100];

	string tmp, finalText;
	int x = 0;

	for (int i = 0; i < (morseText.length()+1); i++) {
		if (morseText[i] != ' ' && morseText[i] != '\0') {
			tmp += morseText[i];
		}

		else {
			convText[x] = tmp;
			x++;
			tmp = "";
		}
	}

	for (int i = 0; i < 100; i++) {
		for (int n = 0; n < size; n++) {
			if (convText[i] == morse[n]) {
				finalText += alphaN[n];
			}
		}
	}

	return finalText;
	
}
Last edited on
Don't worry about it, efficient comes with time and deeper study. The code I gave is 'cute' code anyway, not the 'best way' but 'a slick/cool way'. I basically hard-coded / forced the morse code into a 'binary search tree'. You will run into this later, and maybe revisit what I did then if you think of it.

The most important thing is you took what we told you and got it to work with your own code, that is huge, and something very few students will do. Everything else about your code pales beside that accomplishment.
Last edited on
Registered users can post here. Sign in or register to post.