Recent

Author Topic: [UNSOLVED]What is the problem with this program  (Read 2125 times)

whitehat

  • Jr. Member
  • **
  • Posts: 93
[UNSOLVED]What is the problem with this program
« on: November 24, 2016, 07:58:29 pm »
when i execute my program this error message apparaît (Runtime erro 152 at S000000001000001BFC S0000001000001BFC ligne 79 of project12.lpr $00000000100001CC6)
program app;
uses wincrt;
var
 n:integer;
 ph,phcr:text;

function alpha (ch:string):boolean;
  var
    b:boolean;
    i:integer;
  begin
    b:=true;
    i:=1;
    repeat
     if (ch in ['A'..'Z',' ']) then
      i:=i+1
     else
      b:=false;
    until ((not(b)) or (i>length(ch))) ;
    alpha:=b;
  end;

procedure remplir (var f:text;n:integer);
 var
    ch:string;
    i:integer;
 begin
    reset(f);
    for i:= 1 to n do
      begin
         repeat
           write('saisir la chaine ',i,': ');readln(ch);
         until (pos('  ',ch)=0) and (alpha(ch));
         writeln(f,ch);
      end;
    close(f);
 end;

procedure crypt(var f,ff:text);
 var
    p,i:integer;
    ch:string;
 begin
    reset(f);
    reset(ff);
    while not(eof(f)) do
     begin
         p:=random(5)+1;
         readln(f,ch);
         for i := 1 to length (ch) do
           if (ch <> ' ') then
            if ord(ch)+p > ord ('Z') then
              ch:=chr(ord(ch)+p - ord ('Z')+64)
             else
              ch:= chr(ord(ch)+p);
         writeln(ff,ch);
     end;
    close(f);
    close(ff);
 end;

procedure affiche (var f:text);
 var
    ch:string;
 begin
    reset(f);
    while not(eof(f)) do
      begin
          readln(f,ch);
          writeln(ch);
      end;
    close(f);
 end;


begin
  assign(ph,'D:\bec\prases.txt');
  assign(phcr,'D:\bec\ph_crypt.txt');     {creation des fichiers}
  rewrite(ph);
  rewrite(phcr);
  repeat
   write('N=');readln(n); {le nombre des chaines}
  until (n<=20);
  remplir(ph,n);    {remplissage de phrase.txt}
  crypt(ph,phcr);   {cryptage des chaines}
  affiche(phcr);    {affichage du fichier crypté}

end.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [UNSOLVED]What is the problem with this program
« Reply #1 on: November 24, 2016, 08:43:19 pm »
In you routines alpha() and crypt() you declare ch as a string.
You later try to access individual characters in ch. This has to be done by indexing.
So
Code: Pascal  [Select][+][-]
  1.  if (ch in ['A'..'Z',' ']) then
needs to be:
Code: Pascal  [Select][+][-]
  1. if (ch[i] in ['A'..'Z',' ']) then
The other places where you also try to access individual characters in the string ch also need to be corrected.

 

TinyPortal © 2005-2018