0
votes

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.

3
until exit=true or exit=false; doesn't this mean the loop end no matter if exit is true or false? - Thanh Pham
yeah indeed, is there anyway to stop the loop from exiting before it meets those cases above, cos personally I think boolean always have a default assigned value... - Hoang Nguyen
until 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 Pham
Alternatively, you can loop indefinitely (while true) and break it when one case is met. - Thanh Pham
thanks will give it a try :) - Hoang Nguyen

3 Answers

0
votes

As my understanding, you only want the loop to end when user enters some specific strings.

It can be achieved by modifying the until condition like this:

choice='yes' or choice='y' or choice='t' or choice='true' or choice='no' or choice='n' or choice='f' or choice='false'

Or alternatively, create an indefinite loop and break it when user enters an expected string:

while true do
  ...
  'yes','y','t','true':
    begin
      exit := true;
      break;
    end;
  'no','n','f','false':
    begin
      exit := false;
      break;
    end;
  ...
end;
0
votes

Your loop terminates when exit=true or exit=false. As exit can only ever be one of those two values, it will always meet that condition, so it will never run your loop.

But also, consider explicitly setting the value of exit := false before starting your loop.

0
votes

What you're asking here is "nullable" boolean thing (value is true, value is false, value is not provided). As far as I know it's not implemented in any Pascal dialect. So that, you have to split your indication into two separate flags: a) is there any well-formed input provided by user; b) is the input is recognized as true or false

function ReadBoolean(prompt: String): Boolean;
var
    choice: String;
    exit: boolean;
    recognized: boolean; { this is our termination flag }
begin
    recognized := false; { we place it to false initially as no user input recognized yet }
    repeat
    begin
        WriteLn(prompt);
        ReadLn(choice);
        case choice of
        'yes','y','t','true': begin exit := true; recognized := true; end; { we mark it as recognized }
        'no','n','f','false': begin exit := false; recognized := true; end; { we mark it as recognized }
        else
            WriteLn('Not a boolean input. Enter again: ');
        end;
    end;
    until not recognized; { we keep asking for user input until known input provided }
    result := exit;
end;