Thread: execute until user presses a key?

  1. #1
    Unregistered
    Guest

    execute until user presses a key?

    other than replying upon CTRL-C (which kills the app), in what platform idependant way can I continue executing code in a loop until the user presses a key?

    all varieties of getc/gets pause until the user hits a key.

    I'm really having trouble with this and would appreciate any suggestions or guidance...

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    There's no truly platform independent way to do this, it either requires use of a nonstandard function such as kbhit in conio.h or a platform specific feature such as resetting the terminal from cooked to raw in Unix.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    From the FAQ in your sig, signal(...) in "signal.h" and have it call a custom handler function looks promising : http://www.eskimo.com/~scs/C-faq/q19.38.html if I settle for CTRL-C; this should be ok.

    I only need this to allow the user to interrupt the program's executing so that they can adjust certain settings before starting it executing again.

    If not, then I guess it is time to look up changing modes or find an existing library to handle multiple systems.

    ,thanks

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Signals are certainly a way to go that is standard C, but C's signal support is icky in general. However, if you manage to do it safely and it works then go for it.

    >If not, then I guess it is time to look up changing modes or find an existing library to handle multiple systems.
    A good idea in the case of non-portable constructs is to define a general macro which is conditionally compiled with the proper platform compatibility:
    Code:
    #if defined UNIX
      #define FUNCTION(arg) unixFunction(arg)
    #elif defined WINDOWS
      #define FUNCTION(arg) winFunction(arg)
    #elif defined DOS
      #define FUNCTION(arg) dosFunction(arg)
    #elif defined LINUX
      #define FUNCTION(arg) linuxFunction(arg)
    #endif
    
    .
    .
    .
    
    /* No problem, the system chooses the right function */
    FUNCTION ( someVar );
    -Prelude
    My best code is written with the delete key.

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Alot of people use EOF.
    All you have to do is something like

    while(fgets(buf, sizeof buf, stdin) != NULL)
    and then when the user on unix hits CTRL-D or on
    windows CTRL-Z fgets will return NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf to return after user presses enter?
    By sp2 in forum C Programming
    Replies: 9
    Last Post: 04-23-2008, 05:33 PM
  2. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  3. mkae it so when the user presses...
    By Rune Hunter in forum C++ Programming
    Replies: 10
    Last Post: 09-27-2004, 10:30 PM
  4. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM