0

Using c++, usleep() and sleep() don't work and currently the timestamp is being measured hundreds of times a second instead of every minute

// CPP program to print current date and time
// using time and ctime.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;
int main () {
for( ; ; ){



    // declaring argument of time()
    time_t my_time = time(NULL);

    // ctime() used to give the present time
    printf("%s", ctime(&my_time));

    }

  return 0;
}
A M
  • 14,694
  • 5
  • 19
  • 44
  • What do you mean by "usleep() and sleep() don't work"? Do you get compilation errors? Show the code where you've tried to use them. – Guy Incognito Jun 13 '20 at 10:06
  • its OSx not windows so sleep() doesn't work, and usleep does work in that the time prints every second but it prints hundreds of entries each time whereas I want one time stamp each minute – roselinda789 Jun 13 '20 at 10:17
  • like the code above but with usleep(1000) after the printf command – roselinda789 Jun 13 '20 at 10:22
  • Maybe this will help you [https://stackoverflow.com/questions/1658386/sleep-function-in-c](https://stackoverflow.com/questions/1658386/sleep-function-in-c) – Flo Jun 13 '20 at 11:12
  • `usleep(1000)` waits for 1000 microseconds (0.001 seconds). To wait for one minute you need `usleep(60000000)`. – Guy Incognito Jun 14 '20 at 09:39

0 Answers0