Search code examples
arraysdelphidelphi-xe6

Array Implementation in Delphi xe6


I want to fill the last three Edit's with the elements of r. Somehow the value of i is not changing. Please have a look!

    procedure TForm1.Button1Click(Sender: TObject);
    var
      r: Array of Real;
      cod,h,N,code,i: Integer;
      value: Real;
    begin


      Val(Edit1.Text, cod, code);
      Val(Edit2.Text, h, code);
      Val(Edit3.Text, N, code);

      Edit1.Text := Edit1.Text;
      Edit2.Text := Edit2.Text;
      Edit3.Text := Edit3.Text;
      setlength(r, N);
      i:= 0;
      while i<= N do
         begin

          r[i] := cod/2 + h*i;
          i := i + 1;
        end;

      Edit4.Text := formatfloat('#.0', r[0]);
      Edit5.Text := formatfloat('#.0', r[1]);
      Edit6.Text := formatfloat('#.0', r[2]);
    end;

    end.

Solution

  • Your code contains a lot of useless and buggy code. So I will fix it as far I can do from the given informations. I added a prefix L to all local variables

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Lr : Array[0..2] of Real;
      Lcod, Lh : Integer;
      LIdx : Integer;
    begin
    
      // throws an exception if Edit1.Text cannot converted to an Integer
      Lcod := StrToInt( Edit1.Text );
      // throws an exception if Edit2.Text cannot converted to an Integer
      Lh := StrToInt( Edit2.Text );
    
      for LIdx := 0 to 2 do
        Lr[LIdx] := Lcod/2 + Lh*LIdx;
    
      Edit4.Text := FormatFloat( '#.0', Lr[0] );
      Edit5.Text := FormatFloat( '#.0', Lr[1] );
      Edit6.Text := FormatFloat( '#.0', Lr[2] );
    end;