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

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the D 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 D programming language

Source code in the d programming language

import std.stdio: stdout, write, writefln;

extern (C) nothrow {
    void _STI_conio();
    void _STD_conio();
    int kbhit();
    int getch();
}

void main() {
    _STI_conio();
    write("Enter Y or N: ");
    stdout.flush();

    int c;
    do {
        while(!kbhit()) {}
        c = getch();

        // Visual feedback for each keypress.
        write(cast(char)c);
        stdout.flush();
    } while(c != 'Y' && c != 'y' && c != 'N' && c != 'n');

    writefln("\nResponse: %c", cast(char)c);
    _STD_conio();
}


  

You may also check:How to resolve the algorithm Find palindromic numbers in both binary and ternary bases step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Empty string step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Axe programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Aime programming language
You may also check:How to resolve the algorithm Bitwise operations step by step in the Action! programming language