Taking Valid Enumeration DataType Input via Loop

68 views Asked by At

I want to take a valid enum data type input from user. An I tried like this. FPC Compiler says only binary datatype cane be compared using '<>' relational operators. Kindly also state in 'Cham_Cham' can I Use Space instead of '_'

I will not go with OOP. If this is the only way then I will go. (In my opinion, it can be done without OOP way. Like without using try-catch.)

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. 
1

There are 1 answers

4
David Heffernan On BEST ANSWER
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.