getch() ??

This is a discussion on getch() ?? within the C Programming forums, part of the General Programming Boards category; what is the actual use of getch () in c?. pls, explain me....

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    14

    getch() ??

    what is the actual use of getch () in c?.
    pls, explain me.

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    I use getch() in programs which needs something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
       
        int ch;
        while ((ch = getch()) && ch != '\r')
        {
            putchar ('*');
        }
        system("Pause");   
    }
    I use getch since my comiler supports it.
    Note that if you try to use '\n' to control the loop it will not work. If you want input until user press "enter" use '\r'.

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Can you at least Google it?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    getch() prompts the user to press a key, and returns the value of that key or alpha (value of 224) if it is not a standard key (such as if it is F1, or a numberpad key). Return equates to '\r' but Alt+Return equates to '\n'. If alph is returned, a second call to getch() will return a value corisponding to the key pressed. For example the up arrow returns 'K' the second time getch() is called.

    Edit, forgot to mention: getch(), does not print the input to the screen. If you want to echo what is typed, use getche().

    Very powerfull function.
    Last edited by King Mir; 06-21-2006 at 05:40 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you just make stuff up, or do you really think you know what you're talking about? It doesn't "prompt" for anything. It simply reads a key, just like getchar does, except for the fact that getchar is buffered input, and is a standard function.


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

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    Quote Originally Posted by quzah
    Do you just make stuff up, or do you really think you know what you're talking about? It doesn't "prompt" for anything. It simply reads a key, just like getchar does, except for the fact that getchar is buffered input, and is a standard function.


    Quzah.
    Perhalps prompt is not the right word. I ment to convey that it will not read from stdin buffer, and thus will always ask for a key, unlike getchar() which will not ask for a key if stdin has something in it. Actually, as I explained it will not ask for a key if the last key returned was alpha.

    The rest is valid. Test it yourself if you do not believe me.

    Getch() is much more than just an unbuffered getchar().
    Last edited by King Mir; 06-21-2006 at 05:28 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by King Mir
    Getch() is much more than just an unbuffered getchar().
    No, not really it isn't. Oh, other than the fact that getch isn't standard, so there are multiple behaviors for it. Compare the <conio.h> version with the <curses.h> version. But no, there really isn't any difference other than the buffering. Even then, the curses version will function very similar to the standard getchar if you set it up right. Your "alpha" as you call it, is nothing special.


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

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    I don't know the cruses version, but the conio version works like this:
    Quote Originally Posted by iRMX® C Library Reference
    getch, getche
    Getch( ) reads a single character from the console without echoing; getche( ) echoes
    the character read.
    Syntax
    #include <conio.h>
    int getch (void);
    int getche (void);
    Additional Information
    Neither function reads <Ctrl>-<C>.
    When reading a function key or cursor-moving key, these functions must be called
    twice; the first call returns 0 or 0xe0, and the second call returns the actual key code.
    See also: cgets( ), getchar( ), ungetch( )
    Returns
    The character read.
    No error return.
    Furthermore, this is exactly how my IDE, DevC++ 4.9.9.2 works. I have used the function extensively.

    0xe0 is the ascii value that prints out as alpha. That is a special value.

    Getch will function as getchar for normal keys, including alphanumeric keys, escape, and a few others. F-keys; numberpad keys; Curser keys; Home, Pg Up, and simmilar keys; delete; and numberpad alternate keys all print out alpha first.
    Last edited by King Mir; 06-21-2006 at 07:46 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    It is probably a bad idea to tell the OP how your specific compiler implements getch() since this information is useless unless he uses the same compiler and programs for the same platform that you do.

    Telling the OP that getch() is an unbuffered getchar() is a correct answer that doesn't make any wrong assumptions about the person asking.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    First of all, DevC++ is a very common IDE, so it is fully relivant how it implements getch().

    Second, although RadiSys Corporation is not the foremost athority on the C librairy, their guide seems to be a valid summary of usefull C functions, and it has not been wrong for me yet. This reference is not part of DevC++ or the Mingw compiler.
    Last edited by King Mir; 06-21-2006 at 07:53 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So exactly what part of "reads a single character from the console" don't you understand? That is what getchar does also.


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

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    From above:
    Quote Originally Posted by iRMX® C Library Reference
    getch, getche
    Getch( ) reads a single character from the console without echoing; getche( ) echoes
    the character read.
    Syntax
    #include <conio.h>
    int getch (void);
    int getche (void);
    Additional Information
    Neither function reads <Ctrl>-<C>.
    When reading a function key or cursor-moving key, these functions must be called twice; the first call returns 0 or 0xe0, and the second call returns the actual key code.
    See also: cgets( ), getchar( ), ungetch( )
    Returns
    The character read.
    No error return.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    How can you get the value of _getch() into a variable?

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    Var = _getch();
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Page 1 of 2 12 LastLast
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