Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Getting Runtime error 2 in Pascal Program

repmeerrepmeer Posts: 2Member
I am trying to solve this puzzle:

LOCK + CANAL + CACHE = HIDDEN

Each letter represents a number from 0 to 9. There is only one solution.

I have found some code somewhere else to solve a problem like these and made some modifications but when i run the program i get a runtime error 2. The code is written in Pascal.
Can someone tell what i'm doing wrong? I have very few programming experience.

[code]program Hidden;

{Solve the cryptogram LOCK
CANAL
CACHE
-----
HIDDEN}

var l,o,c,k,a,n,h,e,i,d :longint;
s,outstr :string;
fout :text;

procedure TryOne;
var x,y :longint;
begin
l:=ord(outstr[0])-48;
o:=ord(outstr[1])-48;
c:=ord(outstr[2])-48;
k:=ord(outstr[3])-48;
a:=ord(outstr[4])-48;
n:=ord(outstr[5])-48;
h:=ord(outstr[6])-48;
e:=ord(outstr[7])-48;
i:=ord(outstr[8])-48;
d:=ord(outstr[9])-48;
if 1001*l + 200*o + 20110*c + k + 2010*a + 100*n + 20*h + e
= 100000*h + 10000*i + 1100*d + 10*e + n
then begin
assign(fout,'HIDDEN.TXT'); append(fout);
writeln(fout,'l=',l);
writeln(fout,'o=',o);
writeln(fout,'c=',c);
writeln(fout,'k=',k);
writeln(fout,'a=',a);
writeln(fout,'n=',n);
writeln(fout,'h=',h);
writeln(fout,'e=',e);
writeln(fout,'i=',i);
writeln(fout,'d=',d);
writeln(fout);
writeln(fout,' ',l,o,c,k);
writeln(fout,' ',c,a,n,a,l);
writeln(fout,' ',c,a,c,h,e);
writeln(fout,'--------');
writeln(fout,h,i,d,d,e,n);
writeln(fout);
writeln(' ', l,o,c,k);
writeln(' ', c,a,n,a,l);
writeln(' ', c,a,c,h,e);
writeln('--------');
writeln(h,i,d,d,e,n);
close(fout);
end;
end;

procedure Permute_it(s :string);
var ss :string;
i,len :integer;
begin
len:=length(s);
if (len=0) then
TryOne
else for i:=1 to len do
begin
outstr:=outstr+s[i];
ss:=s;
delete(ss,i,1);
Permute_it(ss);
Delete(outstr,length(outstr),1);
end;
end;

begin
outstr:='';
s:='0123456789';
Permute_it(s);
end.[/code]

Comments

  • quikcarlxquikcarlx Hollywood, FlPosts: 26Member
    The [color=Purple]2[/color] error is "File not found". Change the [color=Blue]append[/color] function to [color=Red]rewrite[/color]. Better yet, move the assign and rewrite to the beginning of the main part of the program. Also the close right before the last end statement.
  • quikcarlxquikcarlx Hollywood, FlPosts: 26Member
    I rewrote some of your program and tested it out. I also corrected your [color=Orange]if[/color] statement to compute the equality of the 3 words against the 1. I also added in totals for how many times it was tried and how many succeeded.
    [code][{Solve the cryptogram LOCK
    CANAL
    CACHE
    -----
    HIDDEN

    found on Programmer's Heaven that somebody wanted some help with
    on this date 2012-Aug-25

    revised by Carl W. Skipworth Rev Date 2012-Aug-28 }

    program Hidden;

    const
    nz : integer = ord( '0' ); { number zero( in character form ) }
    ten = 10; { worth ten }
    oh = 100; { worth one hundred }
    ot = 1000; { worth one thousand }
    tt = 10000; { worth ten thousand }
    ht = 100000; { worth hundred thousand }

    type
    str10 = string[10];

    var
    l, o, c, k, a, n, h, e, i, d : integer;
    s, outstr : str10;
    fout : text;
    attempts : longint;
    successes : integer;

    procedure TryOne;

    begin
    Inc( attempts );
    l := ord( outstr[ 1] ) - nz;
    o := ord( outstr[ 2] ) - nz;
    c := ord( outstr[ 3] ) - nz;
    k := ord( outstr[ 4] ) - nz;
    a := ord( outstr[ 5] ) - nz;
    n := ord( outstr[ 6] ) - nz;
    h := ord( outstr[ 7] ) - nz;
    e := ord( outstr[ 8] ) - nz;
    i := ord( outstr[ 9] ) - nz;
    d := ord( outstr[10] ) - nz;
    if l * ot + o * oh + c * ten + k + { 1st word }
    c * tt + a * ot + n * oh + a * ten + l + { 2nd word }
    c * tt + a * ot + c * oh + h * ten + e = { 3rd word }
    h * ht + i * tt + d * ot + d * oh + e * ten + n then begin
    writeln( fout, ' l = ',l );
    writeln( fout, ' o = ',o );
    writeln( fout, ' c = ',c );
    writeln( fout, ' k = ',k );
    writeln( fout, ' a = ',a );
    writeln( fout, ' n = ',n );
    writeln( fout, ' h = ',h );
    writeln( fout, ' e = ',e );
    writeln( fout, ' i = ',i );
    writeln( fout, ' d = ',d );
    writeln( fout );
    writeln( fout,' ',l,o,c,k );
    writeln( fout,' ',c,a,n,a,l );
    writeln( fout,' ',c,a,c,h,e );
    writeln( fout,' --------' );
    writeln( fout,' ',h,i,d,d,e,n );
    writeln( fout);
    writeln;
    writeln( ' ',l,o,c,k );
    writeln( ' ',c,a,n,a,l );
    writeln( ' ',c,a,c,h,e );
    writeln( ' --------' );
    writeln( ' ', h,i,d,d,e,n );
    Inc( successes )
    end
    end;

    procedure Permute_it ( s : str10 );

    var
    ss : str10;
    i, len : integer;

    begin
    len := length( s );
    if len = 0 then
    TryOne
    else

    for i := 1 to len do begin
    outstr := outstr + s[i];
    ss := s;
    delete( ss, i, 1 );
    Permute_it( ss );
    Delete( outstr, length( outstr ), 1 )
    end

    end;

    begin
    assign( fout, 'B:HIDDEN.TXT');
    rewrite( fout );
    attempts := 0;
    successes := 0;
    outstr := '';
    s := '0123456789';
    Permute_it( s );
    writeln( fout, ' attempts = ', attempts, ' successes = ', successes );
    writeln( ' attempts = ', attempts, ' successes = ', successes );
    close( fout )
    end./code]
Sign In or Register to comment.