Any simplier way to 'read' enumerated types in pascal?

enums, pascal

Solution

You should be able to just cast the integer to the enumerated type, for example:

t := test(n);

If you want to go the other way, then use `ord`:

n := ord(t);

That should let you move numerically to any item in the list. You can check the bounds with:

Ord(Low(test)))

and

Ord(High(test))

..where `test` is your type.

Problem

User inputs an integer which corresponds to a value of defined enumerated type. I need to assign that value to variable t. This is what I thought of: ``` type test = (red,green,blue,fish); var t:test; n,i:integer; begin readln(n); t:=red; for i:=1 to n do t:=succ(t); end. ``` Am I overcomplicating the task? Is it possible to write a simpler alogrithm?

Original source