Results 1 to 3 of 3

Thread: Variable is continuously assigned 0?

  1. #1
    Join Date
    Jun 2013
    Location
    Scranton
    Posts
    496
    Mentioned
    5 Post(s)
    Quoted
    220 Post(s)

    Default Variable is continuously assigned 0?

    So whenenever I run the following, i is assigned 0 at line 11, and i don't understand why
    Simba Code:
    program new;

    function LtoXP(level: Integer): integer;
    var
      temp, i: Integer;
    begin
      if (level < 1) then exit(-1);
      i := 1;
      while (i < Level) do
      begin
        temp += floor(i + 300 * pow(2, i / 7));
        //temp := temp + floor(i + 300 * pow(2, i / 7));
        inc(i);
        writeln(i);
      end;
      result := floor(temp / 4);
    end;

    begin
      writeln(IntToStr(LtoXP(15)));
    end.

    I change the statement to the equivalent longer version and it no longer does it

    Simba Code:
    function LtoXP(level: Integer): integer;
    var
      temp, i: Integer;
    begin
      if (level < 1) then exit(-1);
      i := 1;
      while (i < Level) do
      begin
        //temp += floor(i + 300 * pow(2, i / 7));
        temp := temp + floor(i + 300 * pow(2, i / 7));
        inc(i);
        writeln(i);
      end;
      result := floor(temp / 4);
    end;

  2. #2
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    You're trying to do Integer += Float, So you're probably getting undefined behaviour. Try casting it to Integer first. I doubt there's an overload for Integer += Float.

  3. #3
    Join Date
    Jun 2013
    Location
    Scranton
    Posts
    496
    Mentioned
    5 Post(s)
    Quoted
    220 Post(s)

    Default

    Quote Originally Posted by Kasi View Post
    You're trying to do Integer += Float, So you're probably getting undefined behaviour. Try casting it to Integer first. I doubt there's an overload for Integer += Float.
    Works, thanks Kasi

    for anyone interested
    Simba Code:
    temp += Integer(floor(i + 300 * pow(2, i / 7)));

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •