How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Seed7 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Seed7 programming language

Table of Contents

Problem Statement

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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Seed7 programming language

Source code in the seed7 programming language

$ 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;

  

You may also check:How to resolve the algorithm Loops/While step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Tree from nesting levels step by step in the Phix programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the SequenceL programming language
You may also check:How to resolve the algorithm Parallel calculations step by step in the J programming language
You may also check:How to resolve the algorithm RPG attributes generator step by step in the REXX programming language