Thread: How to detect if user presses enter?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    How to detect if user presses enter?

    I know this question is dumb, I'm sorry.

    std::cout << "Please press enter (or any key) to continue.";

    // ??? ( <<< what goes here?)

    std::cout << "You pressed enter (or any key)!";

    I've been using the following for all my code so far, but since I'm making a simple text based adventure game, I don't want to create a variable, I just want the user to be able to press enter and continue.

    char x;
    std::cin >> x;
    return 0;

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Like this:
    Code:
    if(cin.get()=='\n')
         //Enter Was pressed 
    else
        // :P :P :P

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    To expand on manasij7479's answer: How to I get my program to wait for a keypress - FAQ You should check all the FAQ's out, there is a lot of good information there.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    I like the keypress! It makes me press enter still, but that way I can just use the function instead of having to type an If + Else statement every time. Thanks!

  5. #5
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Here is an advanced way to read the keyboard.
    RFC: Task Killer
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check if user presses the Enter key
    By 337higgin in forum C Programming
    Replies: 8
    Last Post: 09-19-2011, 08:53 PM
  2. scanf to return after user presses enter?
    By sp2 in forum C Programming
    Replies: 9
    Last Post: 04-23-2008, 05:33 PM
  3. Getting user key presses
    By Mavix in forum C# Programming
    Replies: 4
    Last Post: 08-09-2007, 01:49 PM
  4. mkae it so when the user presses...
    By Rune Hunter in forum C++ Programming
    Replies: 10
    Last Post: 09-27-2004, 10:30 PM
  5. execute until user presses a key?
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-31-2002, 11:31 PM