Thread: get input wihout stopping loop

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    get input wihout stopping loop

    i wanted to make a simple program to keep updating the time until a key is pressed. for example:
    Code:
    while(/*check for key press*/) {
      cout << time;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by linux_kid View Post
    i wanted to make a simple program to keep updating the time until a key is pressed. for example:
    Code:
    while(/*check for key press*/) {
      cout << time;
    }
    ncurses and google are your friends

  3. #3
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    It may be more effective to loop every second rather than allow the loop to spin at the CPU's speed. Instead of waiting for a key press you could end the process by sending an interrupt like control+c. In Linux you can use signal masks and change behavior of signals.. you can even catch a signal and make it stop the loop and print the final time, etc.

    Allow it to infinitely loop and set up a signal handler or another event handler to interrupt the loop and end it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stopping a Loop
    By Catman188 in forum C Programming
    Replies: 2
    Last Post: 02-14-2008, 10:14 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. Input in timed loop
    By glo in forum Game Programming
    Replies: 12
    Last Post: 05-20-2006, 06:04 AM
  4. Infinite Loop when entering invalid input
    By acwheat in forum C Programming
    Replies: 5
    Last Post: 04-18-2006, 04:17 PM
  5. User input while loop is running
    By two31d in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2005, 05:28 PM