1

I'm trying to make a timer which will count from the amount of time the user commands it, to zero.

Now I'm trying to add a pause faction to it, which will require to my programm to accept and read input while the timer ticks.

This is the code I have so 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 and " << minutesLeft << " minutes left." << endl;
    secondsCount=0;
    }
    secondsCount++;
    seconds--;
    Sleep(1000);
    timer();
}
int main()
{
    // introduction and time picking
    cout << "Welcome to my Timer - Please set the amount of hours and than minutes you want the timer to run" << endl;
    double requestedHours, requestedMinutes;
    cin >> requestedHours;
    cin >> requestedMinutes;
    double requestedSeconds = requestedHours*3600 + requestedMinutes*60;
    seconds = requestedSeconds;
    cout << "Timer Started";
    timer();
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Tomer8009
  • 189
  • 2
  • 3
  • 11
  • Time to learn multi threaded programming. http://stackoverflow.com/questions/266168/simple-example-of-threading-in-c. http://en.cppreference.com/w/cpp/thread – R Sahu Jun 15 '15 at 02:30
  • Thanks for the guide, I read it, and when I tried to add #include I got this massage edit - nevermind I think its workable, Ill contact you if Ill have a problem – Tomer8009 Jun 15 '15 at 02:41
  • I could fix it myself nor find it on the web, the problem I have now is that: #ifndef __GXX_EXPERIMENTAL_CXX0X__ #error This file requires compiler and library support for the \ ISO C++ 2011 standard. This support is currently experimental, and must be \ enabled with the -std=c++11 or -std=gnu++11 compiler options. #endif – Tomer8009 Jun 15 '15 at 02:49
  • @Tomer8009 what compiler and environment are you using – M.M Jun 15 '15 at 05:28
  • Codeblocks, but I found the answer by now. thnanks. – Tomer8009 Jun 15 '15 at 13:53

1 Answers1

0
#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 the number of bytes it reads
        if (r > 0)  //something was 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 also be cool

sorry i only have this solution in C

PS: You're computer isnt suppose to work recursively infinitly, you should use a loop instead of an infinite recursion (timer() recalls itself), or your stack will overflow

toss
  • 13
  • 3