Preventing User Input from Keyboard

This is a discussion on Preventing User Input from Keyboard within the C++ Programming forums, part of the General Programming Boards category; I'm writing a program in which I want the program to stop at certain points and wait for the user ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    Preventing User Input from Keyboard

    I'm writing a program in which I want the program to stop at certain points and wait for the user to press Return before continuing.

    So far I've tried cin.get() and cin.getline(), but these have proven unsatisfactory because if the user presses anything other than Return, the program advances too fast.
    I've tried using things like cin.ignore() and cin.clear() but they don't work, and when they do it's incredibly untidy and reliant on the user not typing much when he shouldn't.

    What I really want is a way to prevent the user typing anything except enter, or preferably a way to scan what's been typed before it gets to the screen, and allow it through only if it's one of a few allowable characters (ie, '\n' or numbers 1 - 5 for choosing one of several options.)

    Any help would be greatly appreciated.

  2. #2
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Use do and while loop to accept only one key from user
    Code:
    	do{
    	a = getch();
    	}while(a != 'e');
    change the 'e' to whatever character you want the user have to press in order to pass through
    Hello, testing testing. Everthing is running perfectly...for now

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    The best solution depends on the situation. The following code is useful in that it ignores all characters in the input stream up to and including the newline (which sounds like it is what you want):
    Code:
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    You have to #include <limits> for that to work.

    hdragon's getch() solution will only work on some compilers since it is not standard.

  4. #4
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    yeh.. i got a question of this sort as well.. i was making a console quest.. there was text and you had to press enter to read more and more text.. I used getch() for this and everything seemed to work fine, but I didn't like that when the user pressed enter like 5 times in 1 second, it skips all the other 'press enter to continue' requests.. so was the case with my snake game.. you could press the arrow keys really fast like up, right, up, right, up, right... and the snake would follow that path.. is there a way to just remove the keys pressed from the buffer before getting a new input?
    what does signature stand for?

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    Can you not try using a for(; loop, then start with a getkey function, then have a switch statement...

  6. #6
    1479
    Join Date
    Aug 2003
    Posts
    253
    You can use system("PAUSE") but it's not very portable. I am not sure if it does what you want but you can give it a shot.
    Knowledge is power and I want it all

    -0RealityFusion0-

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    I don't like system("pause") because it adds whacking great 'press to continue' strings when it runs.

    i am in fact making a text adventure thing like, you, Ruski. This means i need the user to be able to enter text when i want them to, but not when i don't.
    Also, i'm gonna introduce things like items and passwords, so i suppose text will have to be entered. I just need a way of stopping what's been entered affecting the rest of the program until it's been screened and error checked.
    My understanding of streams and the like is quite limited, so i have no idea whether or not the answer's obvious and i'm just using functions incompetantly



    Oh, yeah: Thanks for all the help, everyone.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> I didn't like that when the user pressed enter like 5 times in 1 second, it skips all the other 'press enter to continue' requests..

    This question sounds familiar. The one potential solution I know is not portable and involves using sleep/Sleep and kbhit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with error checking on input from user.
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 11:59 AM
  2. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  3. How do I input an unbuffered character via the keyboard???
    By Michael_in_Ohio in forum C Programming
    Replies: 1
    Last Post: 03-23-2008, 12:00 PM
  4. Replies: 4
    Last Post: 04-21-2004, 04:18 PM
  5. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 05:01 AM

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