Thread: getch() / getche()

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    38

    getch() / getche()

    i was using getch() funtion and storing the result in a int variable. But it didn't work properly. I did an 'add watch' on the program to see what the int varibale was.
    int num;
    num = getch();

    the add watch said num took on a value of 50 when i typed in the number 1, it also went to 49 when i typed in the number 2. Why can't getch() handle integers properly?
    #include <Jesus.h>
    It will save your life

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    getch() returns the character you typed in. If you want the actual number value, try:
    Code:
    int theNum;
    theNum = getch( ) - '0';
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    why the - '0'; part in
    theNum = getch( ) - '0';
    what is the - '0';
    #include <Jesus.h>
    It will save your life

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    that causes the function to return the ASCII value of the character, hence a numerical value, rather than the alpha. Example, by this function if i enter "a" i would recieve from the function its numerical ASCII value which i think is 27...its too late i dont member the ASCII value.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Here's an ASCII table.

    Say the user enters '4'. getch() will return 52. In order to get the actual number the user entered, subtract the ASCII value of 0 from it (48). 52-48=4
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @Blanket: Try reading through the FAQ too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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, 05:15 AM
  3. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  4. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM
  5. differences between getch(), getche(), and ungetch()
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2002, 02:08 AM