
-----------------------------------
BigBear
Fri Feb 08, 2008 3:31 pm

Formatting Output and Type Conversions
-----------------------------------
This Tutorial will explain how to formate the output of Strings, Integers and Reals and how to convert variable from one type to another.

Formatting Output

Strings                                                                   %Output
     put "Hello" :10                                                    Hello_ _ _ _ _    % 10 characters
     put "Hello" : 2                                                     Hello                % 5 characters

Integers
     put 541 : 7                                                         _ _ _ _ 541       % 7 characters
     put 541 : 1                                                         541                  % 3 characters

Reals
     put 3.4 : 6                                                          _ _ _ 3.4          % 6 characters
     put 3.4 : 0 : 3                                                     3.400             
     put 3.4 : 10 : 3                                                   _ _ _ _ _ 3.400 %10 characters 
     put 34 : 0 : 3 : 2                                                3.400e+01        % Sci. Notation
--------------------------------------------------------------------------------------------------------
Type Conversion

Type conversion means converting a variable from one type to another.

Real to Integer                                                  %Output
     ceil(x) - returns the smallest integer not less than x.
          ceil(5.3)                                                       5
          ceil(-4.1)                                                      -4

     floor(x) - returns the largest integer not greater than x.
          floor(8.8)                                                     8
          floor(-0.1)                                                    -1

     round(x) - rounds to the nearest integer
         round(3.1)                                                    3
         round(3.5)                                                    4

Integer to Real                                                 %Output
     intreal(x) - returns the value of the integer as a real.
          intreal(25)                                                   25.0

Integer to String                                               %Output
     intstr(10)                                                          "10"

String to Integer                                               %Output
     strint(x) - converts a string x to its corresponding integer.
          strint("100")                                                 100

String to Real                                                   %Output
     strreal("7.4")                                                     17.4

Using strintok to Error Trap
When you prompt the user for a integer you usually want it to be within say between 0 and 100 if you want to calculate the average mark across 4 tests.
Your code will look like this
%Simple Error Trap Program
var mark1 : int
var mark2 : int
var mark3 : int
var mark4 : int
%We can also group variables of the same type together like this
%var mark1, mark2, mark3, mark4 : int
loop
    put "Enter the first four test results."
    get mark1, mark2, mark3, mark4
    %It will keep asking the user for four marks untill they are all between 0 and 100
    exit when mark1 >= 0 and mark1 = 0 and mark2 = 0 and mark3 = 0 and mark4 =0 and mark1 = 0 and strint (mark1) = 0
            and strint (mark4) 