differences between getch(), getche(), and ungetch()

This is a discussion on differences between getch(), getche(), and ungetch() within the C++ Programming forums, part of the General Programming Boards category; can someone help me differenciate the functions getch(), getche(), and ungetch() ? also, I seem to have conflicts when testing ...

  1. #1
    Unregistered
    Guest

    differences between getch(), getche(), and ungetch()

    can someone help me differenciate the functions getch(), getche(), and ungetch() ?

    also, I seem to have conflicts when testing this specific code under MSVC 6. It seems that the output happens after the loop terminates.


    for(int i=0; i<10; i++)
    {
    A[i]=getch(); // A is a vector
    cout<<A[i];
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    The reason you output appears once the loop ends is becuase your output is buffered. On some machines output is buffered, and must be "flushed" to show the output. This is done by either cout << endl; or fflush(stdout);

    The only difference between getch and getche, is that getche will echo the character to the screen, hence the e in the function name.
    ungetch will put a char back, so that the next call to getch/getche will return that char that was put back.


    The following came from a quick search on google.com, these excerpts come from http://www.delorie.com/djgpp/doc/libc/libc_toc.html
    getch
    Syntax
    #include <conio.h>

    int getch(void);

    Description
    A single character from the predefined standard input handle is read and returned. The input is not buffered. If there is a character pending from ungetch (see section ungetch), it is returned instead. The character is not echoed to the screen. This function doesn't check for special characters like Ctrl-C.

    If the standard input handle is connected to the console, any pending output in the stdout and stderr streams is flushed before reading the input, if these streams are connected to the console.

    Return Value
    The character.

    Portability
    not ANSI, not POSIX

    ungetch
    Syntax
    #include <conio.h>

    int ungetch(int);

    Description
    Puts a character back, so that section getch will return it instead of actually reading the console.

    Return Value
    The charater is returned.

    Portability
    not ANSI, not POSIX

    --------------------------------------------------------------------------------


    getche
    Syntax
    #include <conio.h>

    int getche(void);

    Description
    A single character from the predefined standard input handle is read and returned. The input is not buffered. If there is a character pending from ungetch (see section ungetch), it is returned instead. The character is echoed to the screen. This function doesn't check for special characters like Ctrl-C.

    If the standard input handle is connected to the console, any pending output in the stdout and stderr streams is flushed before reading the input, if these streams are connected to the console.

    Return Value
    The character.

    Portability
    not ANSI, not POSIX

Popular pages Recent additions subscribe to a feed

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