Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

getch

tmlutmlu Member Posts: 15
Hi all,
I got a trouble with the following code.

char c;
c=getch();
char buff[100];
cin >> buff;

The keystoke that getch returns will appear again in the next cin-step!
Why and how do I solve this problem?
Thanks a lot!!
Lu

Comments

  • GiantGiant Member Posts: 225
    The Problem is that the computer processes faster than you can life your hand off the key. Getch and cin are not sensitive to a key being pressed a few times or the key being help down.

    There are a few ways that you could get around it

    [code]
    c=getch();
    delay(amount of time, so that the user can lift their finger of the key)
    char buff[100];
    cin >> buff;
    [/code]

    [code]
    if(kbhit())
    {
    c=getch();
    while(kbhit()){}
    }

    char buff[100];
    cin >> buff;
    [/code]

    Although the second one is longer, it is better cause it will always work. In the first eg, if you set the time to 1 second, what happens if the user holds the key for 2 seconds.




    The Giant
    Member of the Stupid Coders
    http://www.stupidcoders.cjb.net

    "Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." --Albert Einstein

  • tmlutmlu Member Posts: 15
    [b][red]This message was edited by the tmlu at 2002-2-14 12:23:26[/red][/b][hr]
    : The Problem is that the computer processes faster than you can life your hand off the key. Getch and cin are not sensitive to a key being pressed a few times or the key being help down.

    Thank you, Giant. :)
    But the problem still occur when I try the second one.
    And I couldn't find the function delay.
    I did find a function named Sleep, which is for WinCE.

    I also trace my code line by line, so there should be no problem of lifting my hand off the key. However, the problem is not resolved.
    I try to call fflush(stdin) before calling scanf, and again is the problem.

    Is it possible that getch and scanf do not share one input buffer?
    Or somehow getch does not get the keystroke out of the buffer?

    Another question: Is there any member function of istream that works like C-style getch? Thanks.


    -Lu


  • tmlutmlu Member Posts: 15
    : [b][red]This message was edited by the tmlu at 2002-2-14 12:23:26[/red][/b][hr]
    : : The Problem is that the computer processes faster than you can life your hand off the key. Getch and cin are not sensitive to a key being pressed a few times or the key being help down.

    Hi all,
    I found an awkward solution to my problem, though I don't know why it works! By invoking system(" ") before scanf, it runs as expected. Can anyone explain to me why? thank you! :)
    -Lu.

  • tmlutmlu Member Posts: 15
    Hi there,
    Guess what? It's due to the implementation of getch by VC++. I compile the same piece of code under TC++, and it runs perfectly. Perhaps TC++ is more reliable.
    -Lu.
  • ManishManish Member Posts: 119
    : Hi there,
    : Guess what? It's due to the implementation of getch by VC++. I compile the same piece of code under TC++, and it runs perfectly. Perhaps TC++ is more reliable.
    : -Lu.
    :

    Which version of VC++ are you working with??

  • tmlutmlu Member Posts: 15
    : : Hi there,
    : : Guess what? It's due to the implementation of getch by VC++. I compile the same piece of code under TC++, and it runs perfectly. Perhaps TC++ is more reliable.
    : : -Lu.
    : :
    :
    : Which version of VC++ are you working with??

    Visual C++ 6.0
  • Pappy1942Pappy1942 Member Posts: 46
    Hi,
    The following comes from Borland's conio.h for its 5.5 compiler.

    __inline int _getch( void ) { return getch(); } /* for MSC
    compatibility */

    /*
    This function will remove from the console event queue, any key-up events that are pending. under Win95 key-up events will incorrectly be translated into real key presses by the OS ReadFile() API call, which scanf() uses.
    The WinNT version of ReadFile() correctly ignores them, of course. The
    problem can occur when getch() is called to get a key, then scanf() is
    called to obtain more input from stdin. On Win95 the key-up event that is still left in the event queue will cause ReadFile() to think that the same key was pressed again. Although, good programming practice dictates that one shouldn't use both getch() and scanf() near each other in the same program, the following function could help alleviate this key-up problem. cscanf() does NOT suffer from this problem since it gets its input via getch() and not ReadFile().

    __flush_win95_keyup_events() should be called before scanf() is called. Keep in mind that you must allow sufficient time for the user to release the key so that the key-up event is inserted before flushing the key-up events. Otherwise, the key-up event will occur
    after scanf() is blocked and waiting inside ReadFile().

    __flush_win95_keyup_events() returns the number of key-up events that it removed from the queue.

    */

    MS as far as I know does not include the function mentioned.
    This doesn't help but at least you know why.


    Pappy
    You learn something everyday.

Sign In or Register to comment.