Thread: getch statement

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

    getch statement

    I am trying to write a program that takes a single character off the keyboard without having to press the enter key. The getch statement is supposed to do that but it does not. I am using Visual Studio 2008.

    As an example:

    char input;

    Code:
    input = getch();
    
    switch (input)
    {
         case 'A':
            // Stuffing.
            break;
         case 'B':
            // Stuffing.
            break;
         case 'C':
            // Stuffing.
            break;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    What is the value of input? (perhaps 'a', 'b' or 'c'?)
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Some people through out the fact that getch() isn't a standard function. But I think if you are going to use non-standard functions, there are better ways to get keyboard input for a Windows system.

    Example:
    Code:
    #include <windows.h>
    #define PRESSED(x)  ((x) & 0x80000000)
    
    int main(void)
    {
      char keys[256];
    
      while(1)
      {
        GetKeyboardState(keys);
    
        if(PRESSED(keys[VK_A]))
          printf("\rA key is down!");
        else
          printf("\rA is not being pressed!");
      }
    
      return 0;
    }
    Last edited by master5001; 10-08-2008 at 04:12 PM. Reason: Implemented an easier to use macro.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So far as I'm aware, there is no conio.h header in Visual Studio, hence no getch function.
    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    So far as I'm aware, there is no conio.h header in Visual Studio, hence no getch function.
    strictly speaking, one doesn't preclude the other in those two statements.

    There is, somewhere in the MS library code, an implementation of getch().

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

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    So far as I'm aware, there is no conio.h header in Visual Studio, hence no getch function.
    What version are you using?
    I've seen it in every version from 6.0 to 2008.
    "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

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I tried it in 2008, although now that it I look at it, the code I was trying used conio, not conio.h.
    That was the problem...
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  3. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  4. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 11:24 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM