Runtime Error 201 at fpc

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;    

Upvotes: 0

Views: 9341

Answers (2)

Marco van de Voort
Marco van de Voort

Reputation: 26356

  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.

Upvotes: 1

Oberon
Oberon

Reputation: 3249

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).

Upvotes: 2

Related Questions