1.11M Members

Error on my large int system

 
0
 

My program doesn't have compile-error but it sometime struck in 201 runtime-error.
I don't know why, please someone help me.

Here is my program:

program TestNumber;
uses crt;
const
  fi='D:\program\text\ipTestNumber.txt';
  fo='D:\program\text\opTestNumber.txt';
type
  proint=record
    len:word; //length of proint.
    val:array[1..256] of byte; //value of proint.
    sign:boolean; //is smaller than zero?
  end;
  dummy=string;
var
  f:text;
  n,m:proint;
  dum:dummy;
//===========================================================================
procedure proinit(d:dummy;exp:proint); forward;

procedure input;
begin
  clrscr;
  assign(f,fi); reset(f);
  readln(f,dum); proinit(dum,n);
  readln(f,dum); proinit(dum,m);
  close(f);
  write(dum);
end;
//---------------------------------------------------------------------------
{library proint}
procedure proinit(d:dummy;exp:proint);
var
  i,j:word;
begin
  if not(d[1]='-') then begin
    exp.sign:=false;
    exp.len:=length(d);
    j:=0;
    for i:=exp.len downto 1 do
      begin j:=j+1; val(d[i],exp.val[j]); end;
  end
  else begin
    exp.sign:=true;
    exp.len:=abs(length(d)-1);
    j:=0;
    for i:=exp.len+1 downto 2 do
      begin j:=j+1; val(d[i],exp.val[j]); end;
  end;
end;

procedure profill(var n1,n2:proint;var lo:word);
var
  l1,l2,i:word;
begin
  l1:=n1.len;
  l2:=n2.len;
  if not(l1=l2) then
    if l1>l2 then begin
      for i:=l1 downto l2+1 do n2.val[i]:=0;
      lo:=l1;
    end
    else begin
      for i:=l2 downto l1+1 do n1.val[i]:=0;
      lo:=l2;
    end;
end;

function proplus(a,b:proint):proint;
var
  l,i:word;
  memo:byte;
begin
  profill(a,b,l);
  memo:=0;
  for i:=1 to l do begin
    memo:=memo+a.val[i]+b.val[i];
    proplus.val[i]:=memo mod 10;
    memo:=memo div 10;
  end;
end;
{endlibrary proint}
//---------------------------------------------------------------------------
procedure output;
var
  r:proint;
  i:word;
begin
  r:=proplus(n,m);
  assign(f,fo); reset(f);
  if r.sign then begin
    write(f,'-');
    writeln; write('-');
  end;
  for i:=r.len downto 1 do begin
    write(f,r.val[i]);
    write(r.val[i]);
  end;
  close(f);
  readln;
end;
//===========================================================================
begin
  input;
  output;
end.
 
0
 

If you step through the code with the debugger, where does it break? 201 is a range-check error IIRC, so probably an index goes out of range.

 
0
 

I run with F7 (in Free Pascal 2.6.2) and it stopped at

procedure output;
var
  r:proint;
  i:word;
begin
  r:=proplus(n,m);
  assign(f,fo); reset(f);
  if r.sign then begin
    write(f,'-'); //<-----------------------------stopped at here
    writeln; write('-');
  end;
  for i:=r.len downto 1 do begin
    write(f,r.val[i]);
    write(r.val[i]);
  end;
  close(f);
  readln;
end;

and warning: runtime error 4209907

 
1
 

reset(f); should be rewrite(f); or append(f); depending on whether you want a new file or append to an existing one.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article