Te aconsejaria realizar los siguientes cambios:
Código Delphi
[-]
while (length(mem) >= i) do
begin
a := 0;
while (length(mem) >= i) and (mem[i] <> ' ') do
begin
if (mem[i] in setnum) or (mem[i] = ',') then
begin
num := Num+mem[i];
a := a + 1;
end;
i := i + 1;
end;
b := 1; ok1:=false; ok2:=false;
while ((length(num) <= a) and (b<=length(num))) do
Un abrazo