c++ - How to tell my program to do something while waiting to get a command from the user? -


i'm trying make timer count amount of time user commands it, zero.

now i'm trying add pause faction it, require programm accept , read input while timer ticks.

this code have far -

#include <iostream> #include <stdio.h> #include <time.h> #include <unistd.h> #include <dos.h> #include <windows.h> using namespace std; //  sleep(5000);  int seconds; int hoursleft; int minutesleft; int secondscount=0; void timeleft () {     hoursleft = seconds/3600;     minutesleft = seconds/60 - hoursleft*60; } void timer () {     if (secondscount == 60)     {     timeleft();     cout << "the amount of time left is: " << hoursleft << " hours , " << minutesleft << " minutes left." << endl;     secondscount=0;     }     secondscount++;     seconds--;     sleep(1000);     timer(); } int main() {     // introduction , time picking     cout << "welcome timer - please set amount of hours , minutes want timer run" << endl;     double requestedhours, requestedminutes;     cin >> requestedhours;     cin >> requestedminutes;     double requestedseconds = requestedhours*3600 + requestedminutes*60;     seconds = requestedseconds;     cout << "timer started";     timer(); } 

#include <stdio.h> #include <fcntl.h> #include <string.h>  int main() {     char    buffer[16];     int     flags;     int     fd;     int     r;      fd = 0; //stdin     flags = fcntl(fd, f_getfl, 0);     fcntl(fd, f_setfl, flags | o_nonblock);     while (1)     {         memset(buffer, 0, sizeof(buffer));         r = read(0, buffer, sizeof(buffer)); //return number of bytes reads         if (r > 0)  //something read         {             printf("read: %d\n", buffer[0]);             fflush(stdin);         }         else    //nothing has been read         {             puts("update timer here");         }         usleep(50000);     }     return (0); } 

using non blocking read on file descriptor can cool

sorry have solution in c

ps: you're computer isnt suppose work recursively infinitly, should use loop instead of infinite recursion (timer() recalls itself), or stack overflow


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -