Problem with keystroke stacking

This is a discussion on Problem with keystroke stacking within the C++ Programming forums, part of the General Programming Boards category; Is there anyway to prevent from keystroke stacking in a program? I'm consistently have a problem with cin.get() where the ...

  1. #1
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50

    Problem with keystroke stacking

    Is there anyway to prevent from keystroke stacking in a program?

    I'm consistently have a problem with cin.get() where the user presses enter either accidently or for some other reason before arriving at the cin.get and then is inadvertently run through to the next block of text.

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    And one day, the mods created the FAQ. And it was good.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    There is a difference between tedious and difficult.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You can also use cin.ignore() to ignore just the newline character or cin.ignore(numeric_limits<streamsize>::max(), '\n') to ignore everything.

  4. #4
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    I'm wondering, what part of that FAQ is the actual script that is flushing the input buffer? It's a little confusing, can someone help me make sense of it?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> what part of that FAQ is the actual script that is flushing the input buffer?
    Code:
    while ((ch = cin.get()) != '\n' && ch != EOF);
    Although I prefer the cin.ignore option I mentioned above.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    How can you have more than one? Can you give a simple example with code and sample user input that shows the problem?

  7. #7
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    The cin.ignore solution (cin.ignore(numeric_limits<streamsize>::max(), '\n')) only ignores the first enter hit, then the next runs through. My problem is that I have some impatient people that hitting enter alot, and so the input buffer needs to be cleared before the are prompted to hit enter, or they will miss some text.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Can you give a simple example with code and sample user input that shows the problem?

  9. #9
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    There is no user input, thats the thing. It's just a simple prompt.

    Code:
    cout<<"When ready, please press enter"<<endl;
    cin.get();

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Even though I meant for you to give an example that can be compiled and run and thay demonstrates the problem, I think I have an idea of what you mean.
    Code:
    #include <iostream>
    
    int main()
    {
        using namespace std;
    
        cout<<"Lots of text, instructions, etc."<<endl;
        cout<<"Lots of text, instructions, etc."<<endl;
        cout<<"Lots of text, instructions, etc."<<endl;
        cout<<"Lots of text, instructions, etc."<<endl;
        cout<<"Lots of text, instructions, etc."<<endl;
        cout<<"\nWhen ready, please press enter"<<endl;
        cin.get();
    
        cout<<"More text, instructions, etc, getting skipped."<<endl;
        cout<<"More text, instructions, etc, getting skipped."<<endl;
        cout<<"More text, instructions, etc, getting skipped."<<endl;
        cout<<"More text, instructions, etc, getting skipped."<<endl;
        cout<<"More text, instructions, etc, getting skipped."<<endl;
        cout<<"\nWhen ready, please press enter"<<endl;
        cin.get();
    
        cout<<"Again, text, instructions, etc."<<endl;
        cout<<"Again, text, instructions, etc."<<endl;
        cout<<"Again, text, instructions, etc."<<endl;
        cout<<"Again, text, instructions, etc."<<endl;
        cout<<"Again, text, instructions, etc."<<endl;
        cout<<"\nWhen ready, please press enter"<<endl;
        cin.get();
    }
    If the user hits enter twice in succession, the second prompt is skipped. If they only do it once, then they have time to read everything in the second set of text. You want to know when they hit enter twice so fast that they can't read the instructions?

    If so, then this cannot really be done in standard C++. It depends on your compiler and OS whether there is an alternative. The reason is that everything runs so quickly that the program cannot know whether the user hit enter a second time before or after you asked them to hit enter to continue.

    If that is not what you mean, then try posting a complete example thatwe can compile and run to see what you are talking about.

  11. #11
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    You got it, except that I don't want to know if they are missing the instructions, I want to avoid that altogether.

    I only want them to move on after seeing the "Please hit enter to continue" prompt.

    Is there anyway I can disable or get the program to completely ignore any enter key hits up to that point?

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    This cannot really be done in standard C++. It depends on your compiler and OS whether there is an alternative. The reason is that everything runs so quickly that the program cannot know whether the user hit enter a second time before or after you asked them to hit enter to continue.

    In other words, as far as the program is concerned, the user does see "Please hit enter to continue" before hitting enter, because in that split second between the user hitting enter the first time and the user hitting enter the second time, your program has already run the command and output the "Please hit enter to continue" prompt to the screen. Remember that your program has no concept of human time.

    So again, what you want is to wait a certain amount of time before allowing the user to hit enter. To do that depends on your OS and compiler. If you tell us what OS and compiler you are using, maybe somebody familiar with your setup can recommend a solution. Possibilities I am thinking of are sleep/Sleep and kbhit.

  13. #13
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    I'm using Dev C++ 4.9.9.2 with the built in MingW compiler.

    I'm going to try right now and see if sleep would help at all...

    Sleep still allows the stacked key hits to go after sleep is done, which still undesirable.
    Last edited by Flakster; 10-20-2005 at 12:53 PM.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    See if you can find kbhit. I would Sleep a certain amount of time and then check kbhit before displaying the second "Press enter to continue". Maybe you can ignore extra newlines that way.

    Sorry, I'm not very familiar with those functions so I cannot offer more help. I'msure you can find details if you search kbhit or _kbhit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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