Thread: handling input while program is running

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242

    handling input while program is running

    Hi folks.If I write a stopwatch program, is it possible for the user to stop the stopwatch(without closing the program) by clicking a key? And then press another key to continue the timer? Does the program need a function to monitor the keyboard while the program is running? Or are C programs uninterruptible once they have started running? I can imagine situations where I would like to interrupt a program while it's processing a for() loop, and then continue the loop at a later time.Thanks.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hello ! If you want to interrupt a loop for a programmer's eye put a scanf function at the end of the loop.Then the program won't continue until the user inputs something.The drawback is that even if you want to continue the loop for 100 times,then you must input something 100 times.

    So maybe you should look here sleep

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The C standard does not have what you want - which is unbuffered input. You can get unbuffered input by working with the operating system, or by using non-standard C libraries, like conio.h.

    The way that conio.h does this is to use a loop to monitor the keyboard. When a key is pressed, the C program can take any action you want.

    As I understand it, the reason for this is that not all devices that run C allow unbuffered input, and the C standard always wants to be device independent. That could not be done if unbuffered input was allowed as part of the C standard.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    Quote Originally Posted by cfanatic View Post
    I can imagine situations where I would like to interrupt a program while it's processing a for() loop, and then continue the loop at a later time.Thanks.
    A possible workaround is to have the for() loop update a status/log file. You will still have to kill the program, but you will be able to continue where you left off by having the program load the status file. Keep in mind the amount of file writes, maybe updating the file only X amount of times.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input file handling
    By mikeman in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2010, 11:42 PM
  2. handling user input
    By rodrigorules in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2009, 06:21 AM
  3. Handling User Input
    By wcfbv in forum C Programming
    Replies: 3
    Last Post: 11-20-2002, 07:35 AM
  4. Help with input handling.
    By Will in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2002, 01:00 PM
  5. Handling input from cin
    By Zarkon in forum C++ Programming
    Replies: 1
    Last Post: 12-17-2001, 08:03 AM