I have a question. I wrote this little program and it was working perfectly, until I changed s
and s2
from string
to ansistring. I need to use ansistring, because it will be way longer than 255 characters. Thanks for your response.
{$H+}
program Test;
uses Crt;
var s,s2:string;
konec,radek:boolean;
i,a,z:integer;
begin
ClrScr;
s:='';
s2:='';
i:=0;
a:=0;
z:=1;
konec:=false;
radek:=false;
repeat
s2:='';
readln(s2);
s:=s+s2;
until s2='';
while konec=false do begin
while radek=false do begin
a:=a+1;
if length(s)+1=a then begin
radek:=true;
s:='';
if a<60 then writeln(s2);
end;
if not (a=length(s)+1) then begin
if s[a]=' ' then
i:=a;
s2:=s2+s[a];
if not (s[a]=' ') then
if a=60 then begin
radek:=true;
delete(s2,i,60-i+1);
writeln(s2);
s2:='';
delete(s,z,i);
end;
if (s[a]=' ') and (a=60) then begin
radek:=true;
writeln(s2);
s2:='';
delete(s,z,i);
end;
end;
end;
radek:=false;
a:=0;
if (s='') then konec:=true;
end;
readkey;
end.
The core thing to know is that shortstring often has no problem with out of bounds string access (s[a] when a is not in the allowed range (a>=1) and (a<=length(s)) )
You set S to '' in this code:
but you don't reset "a". So the following condition is true and the s[a] access bombs out.
How to solve this is left as an exercise to the reader.
Learn to use range checks and you find issues like this in a minute (hint add -Cr -gl to the commandline)