Thread: Press ENTER to exit

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    20

    Press ENTER to exit

    Writing a program...simple thing I can't seem to figure out. In a menu, if a user opts to exit program, the program should then prompt user "press ENTER to exit"...how can I do this? I am trying to make it that it must be ENTER.

    Thanks in adv.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,407
    This cannot be done with standard library facilities as the user will be able to type in other input before pressing enter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    20
    Thanks for the info...however; after digging around even further...found this bit of code that worked perfectly:

    Code:
    fflush(stdin);
        printf("\nPress ENTER to exit...");
        while (getch() != 13)
        {
            printf("\nPress ENTER to exit...")
        }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,407
    Yeah. Note that ffush(stdin) results in undefined behaviour and getch is non-standard. You should also avoid magic numbers, e.g., use '\r' instead of 13.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I really never understood why this features wasn't included within the standard library? This seems like tho such a useful feature and all non standard library provides them :/
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,407
    Quote Originally Posted by ssharish2005
    I really never understood why this features wasn't included within the standard library? This seems like tho such a useful feature and all non standard library provides them :/
    Probably because it requires a notion of interactive input that is too specific for a general standard. Still, I suppose it could have been an optional part of the standard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Press Enter to Exit a Loop?
    By lyelt in forum C Programming
    Replies: 7
    Last Post: 11-03-2014, 01:29 PM
  2. Press Enter to go back, press 0 to exit
    By Deanz Tinio in forum C Programming
    Replies: 5
    Last Post: 06-07-2014, 01:01 AM
  3. Replies: 4
    Last Post: 07-02-2011, 11:18 AM
  4. Exit on key press?
    By dazed in forum C Programming
    Replies: 3
    Last Post: 12-09-2002, 07:46 PM
  5. if i press enter
    By abbynormal87 in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2002, 05:37 PM