Here's the code i've done so far:

PROGRAM MORSE_CODE_TRANSLATOR;
USES CRT;

VAR Sentence : STRING[25]; {Maximum of 25 characters in sentence}
    MorseOut, EnglishIn, EnglishOut : STRING;
	Count : INTEGER; KEY : CHAR;
PROCEDURE AnyKey;Forward;

{~~~~~~~~~~~~~~~~~~~~~~EXTRA PROCEDURES~~~~~~~~~~~~~~~~~~~~~~~~~}
PROCEDURE AnyKey; {Makes the program move on when any alphanumerical key is pressed}
VAR AnyKeyIN : CHAR;
BEGIN {AnyKey}
	 WRITE ('Press ANY KEY to continue ');
	 AnyKeyIN := readkey;
	 WHILE (AnyKeyIN = #0) DO
	 BEGIN {WHILE}
		AnyKeyIN := readkey
	 END; {WHILE}
END; {AnyKey}	 

{~~~~~~~~~~~~~~~~~~~~~~TRANSLATE MORSE~~~~~~~~~~~~~~~~~~~~~~~~~}
PROCEDURE TranslateMorse; {Translates the Morse Code into English}
BEGIN {TranslateMorse}
     IF KEYPRESSED THEN BEGIN
         IF EnglishIn := 'a' THEN MorseOut := '.-';
		  END;	   
END; {TranslateMorse}

PROCEDURE EnglishSentence; {Asks for a English sentence to translate to Morse}
BEGIN {EnglishSentence}
     CLRSCR;
     GOTOXY(40-(LENGTH('---------------------------------------') DIV 2), 2);
     WRITELN ('MORSE CODE TRANSLATOR: ENGLISH SENTENCE');	 
     GOTOXY(40-(LENGTH('---------------------------------------') DIV 2), 3);
	 WRITELN ('=======================================');
	 GOTOXY(40-(LENGTH('-------------------------------------') DIV 2), 5);
	 WRITE ('Enter a short sentence (25chrs Max): ');
	 READLN(Sentence);
	 GOTOXY(30,23);
	 AnyKey;
END; {EnglishSentence}

PROCEDURE SentenceOutput;
BEGIN {SentenceOutput}
     CLRSCR;
	 GOTOXY(40-(LENGTH('---------------------------------------') DIV 2), 2);
	 WRITELN ('MORSE CODE TRANSLATOR: ENGLISH TO MORSE');
	 GOTOXY(40-(LENGTH('---------------------------------------') DIV 2), 3);
	 WRITELN ('=======================================');
	 TranslateMorse;
	 GOTOXY(40-(LENGTH('-------------------------') DIV 2), 6);
	 WRITE(MorseOut);
	 GOTOXY(30,23);
	 AnyKey;
END; {SentenceOutput}	 
	 
BEGIN {MainProgram}
     EnglishSentence;
     SentenceOutput;
END.

Yep. I know. heaps of errors everywhere! I am trying very hard on this one, i've only started programming this year, so i'm not a pro programmer. And I will GREATLY appriciate any answers (prefferably in pascal code) to help get this working.

The funny part is my teacher and classmates reckon this one's easy, I've tried using records, arrays, keypressed, and case / if statements so far, but I've had no success at all, aw.

thanks anyone for any help on this crazy program!

Hello Quagmire,

To finish your program in an elegant way and make it more interactive, I suggest to define a record type like

type
    TMorseRecord = record
              EnglishChar : char;
              MorseChar : string;
    end;

then define a constant array of type TMorseRecord

TMorseDictionary : array[1..26] of TMorseRecord =(
(EnglishChar : 'a'; MorseChar : '.-'),
...//add all english chars and its morse correspondent
)

Define to global arrays InputArray(array of chars) and OutputArray(array of strings).
In the first one you put the input key until the user press #0. In the second one, after you put the new char in the first array, call the function TranslateMorse for that char

function TranslateMorse(aKey :char) : string;
begin
   Result := '';// or EmptyStr if you use Delphi
   for i := 1 to 26 do 
      if aKey = TDictionaryMorse[i].EnglishChar then 
        begin
           Result :=  TDictionaryMorse[i].MorseChar;
           Break; 
        end;
end;
....
OutputArray[i] := translateMorse(anyKey);

At the end, you loop through OutputArray and display it on the screen.

Hope this will help you. If you have other questions, shoot.

Good luck,
Ionut

Hi lonut, thanks for your help, I have a question about the array, my teacher wants the translator to also have "." "," and numbers 0 to 9, so would the array become this?

TMorseDirectory : array[1..38] {inc 0 to 9 and .,} of TMorseRecord =(
EnglishChar : 'a'; MorseChar : '.-'; EnglishChar : 'b'; MorseChar : '...-';

So could the array definition keep going like that until it knows all the letters, numbers and .,?? Also pascal refuses to write more than one morse character on my output screen and I have no idea why..

thanks for helping me,
Quagmire

Since last post i've finished adding all the characters:

TYPE TMorseRecord = RECORD
             EnglishChar : CHAR;
			 MorseChar : STRING;
	 END;

VAR TMorseDictionary : ARRAY[1..38] OF TMorseRecord =(
    (EnglishChar : 'a'; MorseChar : '.-'), (EnglishChar : 'b'; MorseChar : '-...'),
    (EnglishChar : 'c'; MorseChar : '-.-.'), (EnglishChar : 'd'; MorseChar : '-..'),
    (EnglishChar : 'e'; MorseChar : '.'), (EnglishChar : 'f'; MorseChar : '..-.'),
    (EnglishChar : 'g'; MorseChar : '--.'), (EnglishChar : 'h'; Morse Char : '....'),
    (EnglishChar : 'i'; MorseChar : '..'), (EnglishChar : 'j'; MorseChar : '.---'),
    (EnglishChar : 'k'; MorseChar : '-.-'), (EnglishChar : 'l'; MorseChar : '.-..'),
    (EnglishChar : 'm'; MorseChar : '--'), (EnglishChar : 'n'; MorseChar : '-.'),
    (EnglishChar : 'o'; MorseChar : '---'), (EnglishChar : 'p'; MorseChar : '.--.'),
    (EnglishChar : 'q'; MorseChar : '--.-'), (EnglishChar : 'r'; MorseChar : '.-.'),
    (EnglishChar : 's'; MorseChar : '...'), (EnglishChar : 't'; MorseChar : '-'),
    (EnglishChar : 'u'; MorseChar : '..-'), (EnglishChar : 'v'; MorseChar : '...-'),
    (EnglishChar : 'w'; MorseChar : '.--'), (EnglishChar : 'x'; MorseChar : '-..-'),
    (EnglishChar : 'y'; MorseChar : '-.--'), (EnglishChar : 'z'; MorseChar : '--..'),
    (EnglishChar : '.'; MorseChar : '.-.-.-'), (EnglishChar : ','; MorseChar : '--..--'),
    (EnglishChar : '0'; MorseChar : '-----'), (EnglishChar : '1'; MorseChar : '.----'),
    (EnglishChar : '2'; MorseChar : '..---'), (EnglishChar : '3'; MorseChar : '...--'),
    (EnglishChar : '4'; MorseChar : '....-'), (EnglishChar : '5'; MorseChar : '.....'),
    (EnglishChar : '6'; MorseChar : '-....'), (EnglishChar : '7'; MorseChar : '--...'),
    (EnglishChar : '8'; MorseChar : '---..'), (EnglishChar : '9'; MorseChar : '----.')

Apparently this program is meant to output a sentence that the user enters, and when it is output, it must be all in morse code with english appearing underneath, argh!
is that possible?! it seems tricky and difficult to me.

big thanks to lonut and anyone else who replies with some help,
Quagmire

You defined well the array and you can increment the length as much as you need.
About the output, yes, it's possible. Build those two arrays I said and write something like this in your output method:

procedure WriteOutput(aMorseSentence : array[1..100] of string, anEnglishSentence : array[1..100] of Char);
var 
 ...
begin
  xPos := 1;
gotoxy(1, 10);
  for i := 1 to 100 do
    begin
      gotoxy(xPos, whereY - 1);
      write(OutputArray[i]);
      gotoxy(xPos, whereY + 1);
      write(InputArray[i]);      
      yPos := YPos + Length(arr2[i]) + 1;//+ 1 to see a space between morse codes. If you want a full sentences delete +1
    end;
  readln;
end;

read about gotoxy function here


Ionut

Sorry lonut, but the array of all letters and morse codes failed. Can you tell me how to 'fully' define the array that tells it all the letters and codes, because it says BEGIN EXPECTED where i wrote out all the letters and codes. could it just be a massive IF statement or a massive CASE statement, or could they all be constants??

please help,
Quagmire

GOOD NEWS!!!

I got help from a few programming classmates today, program now works and the english to morse code function works fine! I'm now working on getting it to do morse code to english, which apparently is just the reverse of the IF statement i'm using right now to define all the characters. Lonut or anyone, do you know a good way of doing sound? because others in my class are experimenting with sound that would go 'dit dah dit dit' etc, as the code comes on screen.

So again thanks for any help,
Quagmire

This article has been dead for over six months. Start a new discussion instead.