Thread: Loop until a key is pressed

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    6

    Loop until a key is pressed

    I am using visual studio 2008. I am trying to write a loop that continues until a user presses the y or the n key. I can do it if the user types one or the other of these and presses enter, but I want the loop to exit once Y or N is pressed directly. What I am doing is a yes/no prompt like this.

    Code:
    char keyin;
    do
    {
            // Listen for keyboard input.  If Y or N is pressed, terminate loop.
    }
    while (keyin != toupper('y') && keyin != toupper('n');
    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you absolutely have to loop "listening" for a key - would calling getchar() and block your CPU usage until the key actually comes in not be a good plan?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe the OP wants a function that blocks until a key is pressed and then returns directly (without having to press enter), similar to the non-standard getch.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I believe the OP wants a function that blocks until a key is pressed and then returns directly (without having to press enter), similar to the non-standard getch.
    In which case there are functions described in the FAQ on "how to read user input without enter" or something along those lines.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check if a key has been pressed and released
    By xkrja in forum Windows Programming
    Replies: 3
    Last Post: 09-19-2006, 09:06 AM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. When a key is pressed, print to screen this... How?
    By Woggy in forum C++ Programming
    Replies: 5
    Last Post: 05-31-2003, 11:46 PM
  5. RETURN key pressed!!!!!!
    By RISF in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2001, 05:07 AM