enumsfreepascal

Taking valid enumeration data type via input loop


I want to take a valid enumeration data type input from the user. I tried it like this:

program Enum

type Food = (Pizza, Biryani, Halwa, Cham_Cham)

var Choice: Food;

begin
   WriteLn('Pizza, Biryani, Halwa, Cham_cham');

   repeat
    WriteLn('Which Food Do you want to eat?');
    Read(Choice);
   Until Choice <> Food

WriteLn('You can eat: ' + Choice);

end. 

However, the FPC says only binary data types can be compared when using the <> relational operator.

I want to avoid OOP, but if it is the only way then I will. I am hoping it can be done without OOP, like without using trycatch.

Also, is possible to use a space instead of _ in Cham_Cham?


Solution

  • Until Choice <> Food
    

    Food is a type. Choice is a value. You cannot compare a type to a value. That is what the error message is telling you. If you want to compare the entered value to something, it has to be another value of type Food.

    As for your other question, names in the language cannot contain spaces.


    You will likely need to reconsider how you obtain this input. As it stands your code expects the user to type in ordinal values. I doubt you want the user to have to type in numbers. Furthermore this enables them to type in ordinal values that are invalid, that is outside the range of the type.

    A better way might be to ask the user to type in text and then have your code compare the text to the food names, and assign the ordinal appropriately. But I really don't know what your program is trying to achieve and its beyond the remit of this question to say much more.