Results 1 to 2 of 2

Thread: First Script Help.

  1. #1
    Join Date
    Feb 2008
    Posts
    198
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default First Script Help.

    Hey so i was following a tutorial to start learning scar, the tutorial being:

    http://www.villavu.com/forum/showthread.php?t=12154

    Well i just finished section two of it, with the kilometers converter script. which the final result is this:

    program MilesToKilometers;

    function MilesToKilometers(miles:extended):extended;
    begin
    result:= miles * 1.609;
    end;

    procedure WriteFunction;
    begin
    writeln(+floattostr' mile is equal to '+floattostr(MilesToKilometers(1.0))+' kilometers');
    end;

    begin
    writeFunction;
    end.
    Well the script works fine, however i noticed that if you wanted to change the 1.0 to say 10.0 the script would still end up saying"1 mile is equal to 8.045 kilometer" when it should be "10 mile is equal to 8.045 kilometers"


    Could someone teach me how to change it so that if its a 1 it will say 1 mile, if its a 2, 2 mile and kilometerS.. etc etc? i would greatly appreciate it. =]

  2. #2
    Join Date
    Jan 2008
    Location
    Frankfurt, Germany
    Posts
    742
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    The problem here is that "miles" is a "lokal variable", which means that it can't be used in any other procedures or functions other than the MilesToKilometers one. To make this work, you need a global variable, here is an example:
    SCAR Code:
    program MilesToKilometers;
    var
      WriteMiles, WriteKilometers: Extended;

    function MilesToKilometers(miles:extended):extended;
    begin
      result:= miles * 1.609;
      WriteMiles := miles;
    end;

    procedure WriteFunction;
    begin
      WriteKilometers := MilesToKilometers(2.5); // Change it to whatever you want here ^^
      if (WriteMiles = 1) then
      begin
        writeln(FloatToStr(WriteMiles)+' mile equals to '+floattostr(WriteKilometers)+' kilometers');
      end else
      begin
        writeln(FloatToStr(WriteMiles)+' miles equal to '+floattostr(WriteKilometers)+' kilometers');
      end;
    end;

    begin
      writeFunction;
    end.
    This way it will also get the plural and singular right.
    If you need more explanation, let me know
    There is nothing right in my left brain and there is nothing left in my right brain.

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
  •