[comp.lang.pascal] Reuqest : Help with Enumeration Types

Arne.Gehlhaar@arbi.informatik.uni-oldenburg.de (Arne Gehlhaar) (12/10/90)

Hi

I have a problem with enumeration types.  I want to declare a type "measures"
as a list of measurements: (g,kg,lb,oz, etc.) the actual type declaration is
no problem, but I am not allowed to do input/output with them (says the
Turbo Pascal 4.0 compiler)  So what is the use of those types.  Can anyone
help me ??  What do I have to do in order for the compiler to accept input
and output ?

Thanks for any hints

Arne

boogaard@ruunsa.fys.ruu.nl (Martin vdBoogaard) (12/10/90)

In <3983@uniol.UUCP> Arne.Gehlhaar@arbi.informatik.uni-oldenburg.de
(Arne Gehlhaar) writes:

% I have a problem with enumeration types.  I want to declare a type "measures"
% as a list of measurements: (g,kg,lb,oz, etc.) the actual type declaration is
% no problem, but I am not allowed to do input/output with them (says the
% Turbo Pascal 4.0 compiler)  So what is the use of those types.

They make your programs (more) readable. Supposing you're not able to use
enumerated types, you would have to use e.g. 0 for g, 1 for kg, 2 for lb
etc. What looks better:

  type  Measures  =  ( g, kg, lb, oz );      type  Measures  =  0..3;

  var   Weight_unit:  Measures;              var   Weight_unit:  Measures;

  case Weight_unit of                        case Weight_unit of
     g: <statement>;                           0: <statement>;
    kg: <statement>;             or:           1: <statement>;
    lb: <statement>;                           2: <statement>;
    oz: <statement>                            3: <statement>
  end;                                       end;

I'd prefer the first version.

I/O is a problem on most machines (and according to the ISO standard),
though. You'll have to read/write strings and convert them into a value,
valid for your enumerated type, using a case statement like the first
one in the above example.


Martin J. van den Boogaard         | Dept. of Atomic & Interface Physics
                                   | Debye Institute--Utrecht University
boogaard@fys.ruu.nl                | P.O. Box 80.000, NL-3508 TA Utrecht
boogaard@hutruu51.bitnet           | the Netherlands, +31 30 532904
------------------------------------------------------------------------
"I feel that if a person can't communicate,
                  the very least he can do is to shut up." -- Tom Lehrer
------------------------------------------------------------------------

kamal@wpi.WPI.EDU (Kamal Z Zamli) (12/11/90)

In article <3983@uniol.UUCP> Arne.Gehlhaar@arbi.informatik.uni-oldenburg.de (Arne Gehlhaar) writes:
>Hi
>
>I have a problem with enumeration types.  I want to declare a type "measures"
>as a list of measurements: (g,kg,lb,oz, etc.) the actual type declaration is
>no problem, but I am not allowed to do input/output with them (says the
>Turbo Pascal 4.0 compiler)  So what is the use of those types.  Can anyone
>help me ??  What do I have to do in order for the compiler to accept input
>and output ?
>

It is the rule of PASCAL that you can't do input/output with  enumerated
data type......but you can get away with it by using the common data type
i.e integer, char....

for instance , consider the following example:

(* demo program for enumerated data type *)

program Enumerated;

type
 Symbol=(Kg,Inch,Ib);

var
 Measurement:Symbol;
 Choice:char;

begin 
 Writeln (' Choose between kg,Inch,Ib');
 Readln (Choice);
 Case Choice of 
   'K','k':Measurement:=Kg;
   'L','l':MEasurement:='Ib';
   'I','i':Measurement:='Inch';
  end (*case *)

end. (* program *)

I hope this helps.....