Help me with getch() :(

This is a discussion on Help me with getch() :( within the C Programming forums, part of the General Programming Boards category; Please help me with this situation.. What is the meaning of this test: if (!getch()==0x00) getch();...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    Help me with getch() :(

    Please help me with this situation..

    What is the meaning of this test:

    if (!getch()==0x00) getch();

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    if getch() returns something besides 0 then call getch() again.
    If you understand what you're doing, you're not learning anything.

  3. #3
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    In addition to itsme86's explanation, getch() works by halting the application until the user enters a keystroke. getch() then returns the ASCII value of the keystroke as an int. System-wide keystrokes are processed by the operating system before they are returned by getch().

    When getch() receives one of these keystrokes, it returns 0 to the calling program. The subsequent call to getch() returns a value for the key. Entering the following keystrokes will also cause getch() to return 0 to the calling program and return the key value on the subsequent call to getch(). For special keys, the getch function first returns ASCII 0.

    0x00 is the hexadecimal value of 0.

    Additional Documentation: BGI; getch()


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    Thumbs up thank's

    Thank you very much!

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    question..

    When can i use this test or for what is useful ?

    if getch()==0x00) getch();

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    You can't. There's a syntax error in that code.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    i mean if (getch()==0x00) getch();
    sorry for my mistake..please answer me..
    thank's in advance...

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    try the code:
    Code:
    int a = getch();
    int b = getch();
    printf("a = %d, b = %d\n", a, b);
    and hit an arrow key


    (lol... i need to try that out myself - i just remember it was that way in qbasic )

    [edit]
    if tried it now.
    when you hit an arrow key the first getch() will return 224.

    in case you hit Fx (F1, F2,...) the first getch() will return 0!!!


    edit2: lol... i typed error key instead of arrow key - i guess i must be tired
    [/edit]
    Last edited by Raven Arkadon; 02-27-2005 at 04:12 PM.
    signature under construction

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The statement itself is pretty stupid. Break it down into words:
    Code:
    if( !getch() == 0x00 )
    if getch returns a value, which, when ! is applied, is zero...

    If a value is zero, applying ! to it makes it not zero. Otherwise, if the value is not zero, applying ! to it makes it zero. So why don't you just have:
    Code:
    if( getch() )
    However, this doesn't test for extended keys according to Borland. To test for extended keys, it should be:
    Code:
    if( getch() == 0 )
    Because otherwise you're saying: "if getch returns anything other than zero...". Which doesn't test for extended keys at all.


    However...

    This could be getch under curses. In which case, it returns ERR on error, or an integer value corresponding to a KEY_ value. If so, it's still a stupid statement, because they should be testing for ERR.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    n00b
    Join Date
    Feb 2005
    Location
    United Kingd00m
    Posts
    5
    qq: Is getch() just a shortened version of getchar()?

    edit: i realised i could just find out myself... no it isn't
    Last edited by ytrewq; 02-28-2005 at 12:38 PM.

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. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 04:15 AM
  3. Pause a C program without getch()
    By swgh in forum C Programming
    Replies: 4
    Last Post: 02-20-2006, 10:24 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM

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