1

I need to let the user stop the cycle while it is still iterating/looping.

I want my program to show "sec" many times, until the user pauses it.

#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
#define p printf
#define s scanf
int main(void)
{     
    int i,j;
    time_t seconds;
    do
    {
        i=seconds;    
        seconds=time(NULL); 
        j=seconds;
        if(i!=j)
        {
            p("sec");
        }
    }while(j==j);
    getchar(); 
    return(0);
}

Using getch or scanf, will stop the cycle each time. Can some one help me? I was thinking that there might be some function that detects if a key is being pressed, but doesnt wait for it to be pressed to continue the cycle. Of course the j==j is to keep an infinite cycle.

OMG i founf the solution here: c programming check if key pressed without stopping program

If you want i cant post the fisnished program, its for a friend that wants to improve in gun shooting.

Community
  • 1
  • 1
  • Pauses it how, by pressing any key? – Subinoy May 29 '15 at 19:30
  • I don't think that this problem can be solved without using threads. – Subinoy May 29 '15 at 19:34
  • On windows you can use `kbhit`, on *nix systems you need to muck about with the terminal settings so that you can write your own `kbhit` function. – user3386109 May 29 '15 at 19:36
  • BASIC is a programming language. Your question is not about BASIC, and should not be tagged `basic`. – Charles Duffy May 29 '15 at 19:38
  • @user3386109 Or read keyboard events from `/dev/input/event*` – Eugene Sh. May 29 '15 at 19:40
  • 1
    @Subinoy, it most certainly can. Something as simple as the `select()` call will distinguish whether input is available and, if configured not to wait, proceed if not. Asynchronous IO is a thing, y'know. :) – Charles Duffy May 29 '15 at 19:40
  • @EugeneSh., please don't encourage something so nonportable. (Also... does the OP **really** want to wait for a keyboard hit in _any_ window, as opposed to the one they're currently focused in? That would be an unusual requirement). – Charles Duffy May 29 '15 at 19:40
  • @CharlesDuffy sorry for complication, you are right :) – Subinoy May 29 '15 at 19:42
  • @EugeneSh. you have give me a beautiful concept for one of my programming that I was thinking, :-) – Subinoy May 29 '15 at 19:43
  • @Subinoy, I'm actually a bit curious (as someone who once wrote a Python library for parsing evdev syntax -- and multiplexing it out to multiple systems -- and a kernel module to allow easy injection of evdev events from userspace, in service of a tool that simulated input events for GUI testing a lab for embedded appliances). – Charles Duffy May 29 '15 at 22:49

1 Answers1

0

You can do this using a signal handler to handle SIGINT. So that, when user presses Ctrl + C the control will be passed to the signal handler and you choose what to do in it.

void handler(int);
int main(){
    signal(SIGINT, handler);// Register the signal handler
    ...
}

void  handler(int sig)
{
// Your logic to decide what to do when Ctrl + C is pressed
}

Reference: [1] - http://www.csl.mtu.edu/cs4411.ck/www/NOTES/signal/install.html

Sajith Eshan
  • 696
  • 4
  • 17