Morse code translator

Translates text into morse code and vice versa

Page 1 of 1

4 Replies - 23462 Views - Last Post: 10 February 2010 - 10:15 AM Rate Topic: -----

#1 Den Onde  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 09-February 10

Morse code translator

Posted 10 February 2010 - 08:38 AM

Hi!
I'm in kind of a jam and I would really really appreciate all the help I can get. Mind that I am a rookie in the world of c++ and have only taken a crash course for about 4 weeks now.

The problem I am having is that I'm supposed to write a program which translates text into morse and vice versa...

I've managed to make the translation from text to morse work using this code:
#include <iostream>
#include <string>
using namespace std;


	string texttomorse(char c)
	{
	string text = "abcdefghijklmnopqrstuvwqyz"; //osv
	string morse[] = {".-","-...","-.-.","-..", ".", "..-.", "--.",
					  "....", "..", ".---", "-.-", ".-..", "--",
				      "-.", "---", ".--.", "--.-", ".-.", "...", "-",
				      "..-", "...-", ".--", "-..-", "-.--", "--.."}; //osv
	  int index = text.find(c);
	    if(index!=-1)
            return morse[index];
         else
            return " ";
	}

int main()
{
	string ord; 
	getline(cin, ord);
	string morse="";
		for(int i=0; i<ord.length(); i++)
		{
        morse += texttomorse(ord[i]);
		}
		cout << morse << endl;

   return 0;
}



Now to the problem at hand :P
To make the program translate morse to text I hoped it would be as simple as just reversing the process
accordingly:
#include <iostream>
#include <string>
using namespace std;


	string morsetotext(char c)
	{
	string text = "abcdefghijklmnopqrstuvwqyz"; //osv
	string morse[] = {".-","-...","-.-.","-..", ".", "..-.", "--.",
					  "....", "..", ".---", "-.-", ".-..", "--",
				      "-.", "---", ".--.", "--.-", ".-.", "...", "-",
				      "..-", "...-", ".--", "-..-", "-.--", "--.."}; //osv
	  int index = morse.find(c);
	    if(index!=-1)
            return text[index];
         else
            return " ";
	}

int main()
{
	string kod; 
	getline(cin, kod);
	string text="";
		for(int i=0; i<kod.length(); i++)
		{
        text += morsetotext(kod[i]);
		}
		cout << text << endl;

   return 0;
}



It made me painfully aware that it wasn't as simple as that :P

Could anyone pleeeeeeaase help me with what it is I'm doing wrong?

Thx

This post has been edited by NickDMax: 10 February 2010 - 08:53 AM
Reason for edit:: Added/Fixed code tags [code]...your program here...[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: Morse code translator

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Morse code translator

Posted 10 February 2010 - 09:08 AM

The problem that you are going to have here is that you also need the gaps between each of the morse code encodings. If you were presented with just a series of dots and dashes you have the problem of not knowing were each character starts and ends. I would think the simplest way of doing this would be to designate a single space character for letter spacing and a two or three space gap between words.

So
-- .- .-. - -.-- - ... .- .

is Martyn in morse (if I remember my morse code from my navigation days).

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 Den Onde  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 09-February 10

Re: Morse code translator

Posted 10 February 2010 - 09:19 AM

Ok, I understand the problem regarding the difficulty of recognise single characters from a slot.
But I don't quite understand is to how I am supposed to do it?
Do you mean that I should include a ws command so that the program will ignore blank inputs (sorry, I don't know the English term for this :P) ?

Thank you for a quick reply by the way
Was This Post Helpful? 0
  • +
  • -

#4 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Morse code translator

Posted 10 February 2010 - 09:26 AM

Yes, you need to ensure that individual characters in morse are separated by a space character and a word in morse is separated by two or three spaces. You could equally use say a forward slash as a character separator and say comma as a word separator.

--/.-/.-./-/-.--/-,.../.-/.

Does that make it easier to understand.

All you have to do then is to run through the input string, extract the characters, find the alphabetic character that matches the morse sequence and there is your solution. Remember, when transmitting morse code, there is a short gap between letters and a slightly longer gap between words. All we are doing here is finding a mechanism that separates characters and words.

This post has been edited by Martyn.Rae: 10 February 2010 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 Den Onde  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 09-February 10

Re: Morse code translator

Posted 10 February 2010 - 10:15 AM

Ok, I'll give it a shot.
Thanks again, hopefully you won't be hearing from me again... regarding this topic anyway :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1