Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/ C++ programming code

  1. Mar 22, 2013 #1
    Hello there!

    I typed this code and saved it as a .c file

    Code (Text):

    int main()
        int a;
                if (kbhit())
                            printf("%d ",getche());
        return 0;
    This code displays ASCII value of any character entered... The problem is that it being an infinite loop does not exit itself on pressing escape key.. How to handle that exception?
    And what is the equivalent of kbhit() in C++ and in which header?

    Thanks in advance
  2. jcsd
  3. Mar 22, 2013 #2


    Staff: Mentor

    you have to read the keyboard buffer if kbhit() returns 1 then if that character is an escape character then you can break; out of the loop.
  4. Mar 22, 2013 #3
    i can check using if() condition, but how to check for escape character?
  5. Mar 22, 2013 #4


    Staff: Mentor

  6. Mar 22, 2013 #5
    ok.. got it.. thanx.. :)
  7. Mar 22, 2013 #6


    User Avatar
    Homework Helper

    Note that the getch() or any code after the return(0) will never get used.

    Normally to terminate user input in DOS like console windows, an "end of file" character is used, such as ctrl-D (hex 04) on Unix / Linux systems, and ctrl-Z (hex 1A) on MSDOS / Windows (in Dos console window) systems. For Windows / GUI type programs, ctrl-Z is normally the "undo" key.
    Last edited: Mar 22, 2013
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook