Iterating until a key is pressed in C -


this question has answer here:

i need let user stop cycle while still iterating/looping.

i want program show "sec" many times, until 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;         {         i=seconds;             seconds=time(null);          j=seconds;         if(i!=j)         {             p("sec");         }     }while(j==j);     getchar();      return(0); } 

using getch or scanf, stop cycle each time. can 1 me? thinking there might function detects if key being pressed, doesnt wait pressed continue cycle. of course j==j keep infinite cycle.

omg founf solution here: c programming check if key pressed without stopping program

if want cant post fisnished program, friend wants improve in gun shooting.

you can using signal handler handle sigint. that, when user presses ctrl + c control passed signal handler , choose in it.

void handler(int); int main(){     signal(sigint, handler);// register signal handler     ... }  void  handler(int sig) { // logic decide when ctrl + c pressed } 

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


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -