Runtime Error 201 at fpc

风流意气都作罢 提交于 2019-11-27 08:45:59

问题


i have been writing a game about guessing numbers and i have to seperate a 4 digit number into its digits and put digits into an array.However that section keeps giving me runtime error 201 using fpc.However when i use ideone.com it gives me what i want.I can't figure out.can it be a bug?Sorry for my English.

program game;
var
    number : array [1..4] of integer;
    z, i, j: integer;
    number_4digit: integer;
begin
     readln(number_4digit);
     for i := 4 downto 1 do begin
        j := i;
        z := number_4digit;
        while z > 10 do begin
            z := z div 10;
     end;   
     number[5-i] := z;
     repeat
           z := z * 10;
           j := j - 1;
     until j = 1;
     number_4digit:= number_4digit - z;
     write(number[5-i], ' ');  
end;    
end.

Edit:I solved the problem.Thanks for Marco van de Voort.

repeat
      z := z * 10;
      j := j - 1;
until j = 1;

I changed this section into this.

while j > 1 do begin
 z := z * 10;
     j := j - 1;
end;    

回答1:


  1. J is always 1 after the for loop.
  2. Then in the repeat loop it is decremented (to j=0).
  3. Which is unequal to 1, so it decreases once more to -1 till -32768 then it rolls over to 32767
  4. then further 32767 to 1.

In summary the repeat is done 65536 +/-1 times. The meaning of the J variable is not clear to me from the code. Comment more.




回答2:


Runtime error 201 is a range check error.

Compile with -gl and you will see where the program crashes in the runtime error. It's line 16 (z := z * 10;), meaning that your z is overflowing. Note that integer is a signed 16 bit type in FPC (maximum 2^15 - 1 = 32767).



来源:https://stackoverflow.com/questions/20315852/runtime-error-201-at-fpc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!