getch statement

This is a discussion on getch statement within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a program that takes a single character off the keyboard without having to press the ...

  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
    Katy, Texas
    Posts
    2,309
    What is the value of input? (perhaps 'a', 'b' or 'c'?)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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, 04:15 AM
  4. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 10: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, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21