Morse to english problems c++

Hello!

I have abit of a problem translating morse to english.

When I write my morse code like ".-" (which means A) my program writes out ab instead of just A.

I've tried alot of difrent things but can't seem to get it working!

My code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;


	string morsetotext(char c)
	{
		string text[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "q", "y", "z"}; //osv
			string morse = ".-""-...""-.-.""-.."".""..-.""--.""...."".."".---""-.-"".-..""--,-.""---"".--.""--.-"".-.""...""-,..-""...-"".--""-..-""-.--""--.."; //osv
	  int index = morse.find(c);
	    if(index!=-1)
            return text[index];
         else
            return 0;
	}


Any help would be appreciated!

Thanks
Anna

I see a few confusions here.

Firstly, your morsetotext function claims to return a string, but you have one path where it will attempt to return the number zero. This is bad. You should return a string.

Secondly, there is a fatal flaw in your logic. Let us suppose that you successfully find the string "-.-."; that is, the morse letter c. The value of index returned will be 6, because -.-. is at index 6 in your string morse - string morse looks like this: .--...-.-.-.. and so on, and you can see there that -.-. begins at the 6th letter.

When you assemble a string as follows: string a = "some " "letters" " th" "at are" " etc";
it is the same as string a = "some letters that are etc";

When you then grab text[6], you get 'g', but you wanted 'c'.

Here is simple code that illustrates this when it is run:
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
#include <iostream>
#include <string>
using namespace std;


	string morsetotext(string c)
	{
		string text[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
                                        "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "q", "y", "z"}; //osv
		string morse = ".-""-...""-.-.""-.."".""..-.""--.""...."".."".---""-.-"".-..""--,-."
                                         "---"".--.""--.-"".-.""...""-,..-""...-"".--""-..-""-.--""--.."; //osv
	  int index = morse.find(c);
	  cout << "Morse found at index location " << index << endl;
	    if(index!=-1)
            return text[index];
         else
	   return (string("Not found"));
	}

int main ()
{
  string c = "-.-.";
  cout << morsetotext(c) << endl;
  return 0;
}
  
Last edited on
Thanks for your reply!

I think I have figured it out now!
Or not!

Now my input string is messing about

It looks like this.

1
2
3
4
5
6
7
	string c; 
	cout << "Write your code: ";
	cin >> c ;
	string reslt;
	reslt = morsetotext(c);
	cout << "Text: " << reslt << endl;
	return 0;


my new morsetotext code looks like 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
#include <iostream>
#include <string>
using namespace std;
string morsetotext(string morse);
string t[] = { "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" };

string m[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
	".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
	"...-", ".--", "-..-", "-.--" };




string morsetotext(string morse)
{
	string temp = "";
	string text = "";
	for(int i = 0; i < morse.length(); i++)
	{
		if(morse.substr(i, 1) != " ")
		{
			temp += morse.substr(i, 1);
		} else {
			for (int j = 0; j < 29; j++) {
				if (temp == m[j]) {
					text += t[j];
				}
			}
			temp = "";
		}

		if(temp == "")
			text += " ";
		
	}
	return text;
}


Thanks for any help!

I figured it could be a good idea to write what was wrong aswell.

If i write .- it should write A, but it doesnt.

It worked when i tried it out by it self.
Last edited on
Are you familiar with the map container? It is exactly the tool you are looking for; you build a map, which essentially just associates two objects, and then you simply search for the object you have (e.g. you look in the map for ".-") and you get back the corresponding other object (e.g. "A").

In your code, you seem to have fewer morse letters than you should have, which won't help :) I think you missed Z, "--.."

Here is working code using two maps, one for each direction.


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


using namespace std;

string t[] = { "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" };

string m[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
	".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
	       "...-", ".--", "-..-", "-.--", "--.." };


int main()
{
  map<string, string> morseToTextMap; // Create a map
  map<string, string> textToMorseMap; // Create a map
  map<string, string>::iterator iter; //Create an iterator to use on the maps. Because the maps
                                        // are of the same type, map<string, string>, I can use the same 
                                       // iterator


  // Build the map
  for (int i=0;i<26; ++i)
  {
    morseToTextMap.insert(pair<string, string>(m[i],t[i]));
    textToMorseMap.insert(pair<string, string>(t[i],m[i]));
    
  }
  
  // Get morse string to look for
  string userInput;
  cout << "Please enter morse letter to convert: ";
  cin >> userInput;
 

  iter = morseToTextMap.find(userInput);
  if (iter != morseToTextMap.end()) 
    {
       cout << "OUTPUT: " << iter->second << endl;
    }
  else
    {
      cout << "Not found " << endl;
    }

  cout << "Please enter letter to convert: ";
  cin >> userInput;

  iter = textToMorseMap.find(userInput);
  if (iter != textToMorseMap.end()) 
    {
       cout << "OUTPUT: " << iter->second << endl;
    }
  else
    {
      cout << "Not found " << endl;
    }

   
  return 0;
}


Last edited on
No, I've never used a map.

Im kinda of desperate, I have to be finished before midnight and at the moment it doesnt seem to be ready in any way.

Im at a complete standstill, I tried using getline, but it still doesnt write anything.

I don't think I have the time to learn using maps, so I will have to use what I have done and try to fix it.

Unless some kind person would help a damsell in distress and do it for me.

Thanks for all the help, atleast i have come some steps closer to completeing it.
Unless some kind person would help a damsell in distress and do it for me.


Is that complete, working code using map not suitable? It does exactly what you're trying to do.
oh, thanks!

How do i make it translate more then 1 letter though? xD

Your my saviour ;)

Your code above wasn't far off, but you made it far more complicated that you needed to. Here's a rather simpler version that handles one letter at a time.

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
#include <iostream>
#include <string>
using namespace std;
string morsetotext(string morse);
string t[] = { "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" };

string m[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
	".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
	"...-", ".--", "-..-", "-.--" };

string morsetotext(string morse)
{
    for (int i=0; ++i; i<26) // For each value, from the first to the last
    {
      if(morse == m[i])  // Check to see if we've found the input morse letter
	{
          return t[i];  // and return the alphabet letter in the same position
	}
    }

  return ("Not found");
} 

int main()
{
  string userInput;
  cout << "Enter morse letter to translate: " << endl;
  cin >> userInput;

  string letter = morsetotext(userInput);
  cout << "Letter is: " << letter << endl;

  return 0;
}


More than one letter - how are the input letters separated?
Last edited on
uhm, by eh.. I don't know really. Im both lost and tierd. :(

Do you mean the user enters one letter, and gets the answer, and then enters another letter, and so on, or is the user to enter lots of letters at once?
Im supposed to translate ".-- -.-- -.- .-" wich means "dyka" (dive in swedish)

Go on then. Because it's Easter. Happy Easter. :)

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
#include <iostream>
#include <string>
using namespace std;
string morsetotext(string morse);
string t[] = { "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" };

string m[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
	".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
	       "...-", ".--", "-..-", "-.--", "--.." };

string morsetotext(string morse)
{
     for (int j=0; j<25; j++)
    {
       if(morse == m[j])
	{
          return t[j];
	}
    }

    return ("#"); // Not recognised letter
} 

int main()
{
  string userInput;
  cout << "Enter morse code to translate, with a space between letters: " << endl;
  getline(cin, userInput);

  string builder;

  // This for loop examines each input letter in turn, and extracts the
  //   morse groups by looking for the spaces. When a space is found, 
  //   the assembled morse letter is sent for translation
  for (int i=0;i<userInput.size()+1;++i)
    { 
      if( (userInput[i] == ' ') || // space between letters
	  (i == userInput.size() ))  // last letter
	{ 
          // send this one to the translator
	  string translated =  morsetotext(builder);
          cout << translated;
	  // and blank the builder
	  builder.clear();
	}
      else
	{
	  // add this letter to the string being built
	  builder = builder + (userInput[i]);
	}  
    }

   
  return 0;
}
Last edited on
Thanks a bunch! happy easter too you to!

Just a quick one, the getline doesnt seem to work for me, it just skips over it :S I dont know why :(

But thanks for all your help!
It is working now!

Thanks for all your help! :D

Happy Easter and all that, now when i've finally sent it in I am going to bed!

Thanks yet again!
Topic archived. No new replies allowed.