c++ - how to increment or decrement from variable until get char -
i wanted create snake program in console application. in 1 confused. problem this:
#include <iostream> #include <cstdlib> #include <ctime> #include <conio.h> using namespace std; int main() { int a=2000; while (1) { cout << time(0)<<endl; -= 100; _sleep(a); **if (getch())break; else continue; } }
my problem in **, want ,to while loop until pressed 1 key. example want run this: print time(). = 1900. wait 1.9s. print time(). a=1800. wait 1.8 s. // pressed key. break.//end program
so found answer used kbhit() in conio.h library.
you may try this. of course need in parent process have getchar() return key press without waiting, such using _getchar().
#include <iostream> #include <cstdlib> #include <ctime> #include <signal.h> #include <conio.h> using namespace std; int main() { pid_t id; id = fork(); if(id) { getchar(); kill(id, sigterm); } else { int a=2000; while (1) { cout << time(0)<<endl; -= 100; _sleep(a); /*if (getch())break; else continue; */ } } }
Comments
Post a Comment