Jump to content

Timer in C++

Featured Replies

All I need is to run a certain section of my code (that loops forever) for a certain amount of time, about 10 seconds. The obvious problem is when it gets to that section, it loops forever and doesn't move on. Is there a function that will allow me to run a given chunk for a given amount of time?

 

Thanks

Of course there is runs for 10 seconds.

#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>

int main() {

    int b=time(0)+10;
    while(b>time(0))
    {
            std::cout << time(0) << std::endl;
    }
}

Edited by fiveworlds

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.