I'm currently working on an exercise of testing Boolean value from user input, as being presented below:
function ReadBoolean(prompt: String): Boolean;
var
choice: String;
exit: boolean;
begin
repeat
begin
WriteLn(prompt);
ReadLn(choice);
case choice of
'yes','y','t','true': exit := true;
'no','n','f','false': exit := false;
else
WriteLn('Not a boolean input. Enter again: ');
end;
end;
until exit=true or exit=false;
result := exit;
end;
It is expected to keep looping asking for value until it receives the input from specified string, however at my first attempt when I try to input 'fred' the boolean variable is automatically assigned as TRUE and exit the function.
Any helps would be very appreciated.
until exit=true or exit=false;doesn't this mean the loop end no matter ifexitistrueorfalse? - Thanh Phamuntil choice='yes' or choice='y' or choice='t' or choice='true' or choice='no' or choice='n' or choice='f' or choice='false';should work although it could be too lengthy. - Thanh Phamwhile true) and break it when one case is met. - Thanh Pham