Previous topic | Next topic | Ada Home Page | Index

Using records

Referring to records and fields

To refer to an entire record variable (for assignment, parameter, comparison, etc) just use its name

To refer to a field of a record, use record_name.field_name

    average_male.weight
    average_female.name

Remember, use the name of the record variable, not the record type


Operations on records

Assignment

You can assign one record variable to another of identical type

    that_person := this_person;
 


Input

You cannot read an entire record variable in a single operation. You must read each field separately.

To input a record variable use a procedure:

    procedure get_person_record(a_person: out persons) is

       space  : constant CHARACTER := ' ';   -- a space char
       nchar  : NATURAL;                     -- for GET_LINE

    begin -- get_person_record

       a_person.name := (others => space);   -- clear name
       PUT("Enter name  "); GET_LINE(a_person.name,  nchar);

       a_person.phone := (others => space);  -- clear phone
       PUT("Enter phone "); GET_LINE(a_person.phone, nchar);

       PUT("Enter sex   ");
       safe_get_sex(a_person.sex );

       PUT("Enter age. ");
       gen_int_input(a_person.age, min_age, max_age);

       PUT("Enter weight. ");
       gen_float_input(a_person.weight, min_weight, max_weight);

    end get_person_record;


Output

You cannot display an entire record variable in a single operation. You must display each field separately.

To display a record variable use a procedure:

    procedure show_person_record(a_person: in persons) is

    begin
       PUT("Name    "); PUT_LINE( a_person.name );
       PUT("Phone   "); PUT_LINE( a_person.phone );
       PUT("Sex     "); PUT(a_person.sex, SET => LOWER_CASE);
       NEW_LINE;
       PUT("Age     "); PUT(a_person.age, WIDTH => 3);
       NEW_LINE;
       PUT("Weight  "); PUT(a_person.weight, EXP => 0, AFT =>2);
       NEW_LINE;
    end show_person_record;


Comparisons

You can compare one record variable to another of identical type using "is equal to" or "is not equal to" operators

    if this_person = that_person then

You should use a function to compare specific fields

    function is_heavier_than(a_person,
        another_person : persons ) return BOOLEAN is

    begin -- is_heavier_than
        return a_person.weight > another_person.weight;
    end is_heavier_than;

To use this function:

    if is_heavier_than(this_person, that_person) then
        PUT(this_person.name); PUT_LINE(" is heavier.");
    else
        PUT(that_person.name); PUT_LINE(" is heavier.");
    end if;


Previous topic | Next topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 27 Feb 96