-2

I am making simple encrypt/decrypt program in Lazarus with 2 buttons, one for encrypt and one for decrypt. I also have two memo boxes (or just two memos ). I tested my algorithm and it works but when i tried implementing it in user friendly app i got this problem.

I have this function:

function enc(x:string):string;
var
   y:string;
   p,q:integer;
   m:char;
begin
y:=x[1];
for p:=2 to Length(x)do
    begin
      q:=p-1;
      if chr(ord(x[p]))=' ' then
            m:='!'
            else  if ord(x[p])>ord(x[q]) then
                     m:=Succ(chr(ord(x[p])))
                        else
                             m:=Pred(chr(ord(x[p])));
         Y:=y+m ;
    end;
enc:=y;
end;

and one procedure to call this function

procedure TForm1.Button1Click(Sender: TObject);
begin
   Memo1.Text:=enc(Memo2.Text);
end;

I compiled program and it worked but when i tried to enter some text and encrypt that text i got this error

exception class 'External: SIGSEGV'

In file 'unit1.pas' at line 46: y:=x[1];

I recently started to learn pascal and to use lazarus soo sry if this Q is stupid but i really want to know what I'm doing wrong.

Community
  • 1
  • 1

1 Answers1

1

That error, for that code, implies that x is an empty string. If the first character is not valid, then what else could x be, other than empty?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I actually realized what the problem was. –  Nov 16 '16 at 22:16
  • procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Text:=enc(Memo2.Text); end; That piece od code made my program stops couse i was typing text into memo1 but with that code i was suposed to type in memo2 so i just replaced memo1 and memo2 and now everythink works well –  Nov 16 '16 at 22:17
  • Which is what I told you in the answer. – David Heffernan Nov 16 '16 at 22:21
  • A useful tip for beginners, rename your components and variables to something meaningful, like `memInput` and `memOutput`. That would have helped you track this down sooner. – David A Nov 16 '16 at 23:12