-1

Good day, I need advice on a piece of code I wrote. It is an activity from my delphi7 school textbook, so you can imagine I am still new to pascal. My program works fine and gets the results I need, but I receive a strange: " Invalid floating point operation" message in runtime without the program crashing. With my limited knowledge, I can't find the reason behind this error. It is confusing as I did not use any real numbers but please have a look. Let me know if you need additional information Thank you in advance, I am open to learning!

   private
    arrLearners : array of string;
    arrMod : array of string;   
procedure TfrmPortfolios.btnEnterClick(Sender: TObject);
var
numL,k,d,a : integer;
l :string;
begin
  k:=0;
  numL:=0;

  repeat
   numL := StrToInt(InputBox('Enter names','How many learners in Subject?
   (Max 30):','30'));
   until
   numL<=30;
 SetLength(arrLearners, numL);

 for k:=0 to numL-1 do
   begin
        arrLearners[k]:='Student No'+IntToStr(k+1);
   end;

 k:=0;
 d := numL div 5; //used to see for every which learner will go to moderation. I.e if d=3 then every third student.
 if numL <=9 then // below 9, d=1 then it's every student anyways
    begin
         for k:=0 to NumL-1 do
            begin
                 SetLength(arrMod,NumL);
                 arrMod[k]:=arrLearners[k];
            end;
    end
    else
      begin
           a := numL div d;
           SetLength(arrMod, a);
           for k:=0 to a-1 do
               begin

                    arrMod[k] := arrLearners[(k*d) +1];
               end;
       end;

 k:=0;
 redOutput.Lines.Add('Students sent for moderation:'+#13);
 for k:=0 to numL-1 do
   begin
        l:= arrMod[k];
        redOutput.Lines.Add(l);
   end;

end;
4
  • I noticed my results were wrong, but I fixed it with and I still get float error: arrMod[k] := arrLearners[(k*d)+d-1 ]; Apr 14, 2018 at 10:51
  • If you're running from the Delphi 7 IDE, did it pop you into a particular place in your code after the message? None of the code you show as any explicit floating point values or operations, so the problem is likely somewhere else.
    – lurker
    Apr 14, 2018 at 18:42
  • FWIW, there are a few useful links that show people how to debug. Here is one: Delphi debugging for dummies , here is another one: Debug faster, Debug smarter both from different CodeRage events. Apr 20, 2018 at 11:08
  • Note that when numL is higher than 9, arrMod size is numL div d. (that is numL div 5). So it's less than numL. However, in the last lines you're looking up the values of arrMod from 0..numL-1 - so you're trying to access members higher than the actual size of the array. That can lead to unexpected behavior ("out of bounds" issue)
    – uri2x
    Apr 26, 2018 at 16:03

1 Answer 1

0

Make sure you have enabled range checking and overflow checking options. You can enable them either globally or locally in the code using compiler directives {$R+,O+} Range check is a very useful thing - it can save you much time on debug weird issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.