

Task

Obtain a valid   Y   or   N   response from the keyboard.

The keyboard should be flushed, so that any outstanding key-presses are removed, preventing any existing   Y   or   N   key-press from being evaluated.

The response should be obtained as soon as   Y   or   N   are pressed, and there should be no need to press an   enter   key.




$ include "seed7_05.s7i";
  include "keybd.s7i";
 
const func boolean: yesOrNo (in string: prompt) is func
  result
    var boolean: yes is FALSE;
  local
    var char: answer is ' ';
  begin
    while keypressed(KEYBOARD) do
      ignore(getc(KEYBOARD));
    end while;
    write(prompt);
    repeat
      answer := lower(getc(KEYBOARD));
    until answer in {'y', 'n'};
    yes := answer = 'y';
  end func;
 
const proc: main is func
  begin
    writeln(yesOrNo("Press Y or N to continue "));
  end func;