Thread: [HELP] key press

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    29

    [HELP] key press

    Code:
    #include <conio.h>
    #include <stdio.h>
    
    int main()
    {
    int bal,wit,dep;
    
    printf("enter balance: ");
    scanf("%i",&bal);
    printf("[W]=Withdraw [D]=Deposit\nPress a key to continue");
    
    /*
    if(kbhit()=)
    {
    }
    
    
    if(kbhit()=)
    {
    }
    */
    
    getch();
    }
    I want the user to press either the W or the D key, W for withdrawing, D for depositing. If the user press the W key, it will immediately prompt the user for withdrawal amount (without typing the alphabet W then press ENTER key), how can I do that? Is kbhit() is right function? Btw, I'm learning C on Windows platform.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    kbhit, if it exists at all in the system (it is not a standard function, so compiler producers do not need to provide it), will tell you if there has been a key pressed or not (that is, the user "hit" a key on the KeyBoard).

    To be standard compliant, use scanf() or getchar().

    --
    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
    Registered User
    Join Date
    Apr 2009
    Posts
    29
    if i use scanf() or getchar(), user have to press ENTER.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dakaa View Post
    if i use scanf() or getchar(), user have to press ENTER.
    Correct. If you do not want that, you need to use non-standard solutions. getch() will do it if it, if your code compiles with it.

    --
    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.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    29
    getch() or getche() worked.
    Last edited by Dakaa; 05-29-2009 at 11:53 PM.

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    It won't work everywhere. But if ONLY you are going to use this, it won't matter.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM