Hack Forums
[C] [C++] Making a timed loop - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] [C++] Making a timed loop (/showthread.php?tid=5156871)



[C++] Making a timed loop - theChive - Feb 2, 2016

I want to make a loop in C++ that prints a string every...lets say 30 seconds.

I'm learning Obj-C programming now and want to tackle the project above.
I started looking around and am not sure if I'm on the right path.

Code
#include <math.h>
#include <time.h>
#include <iostream>

int main(){
  clock_t x;
  x = clock();
  std::cout<<"Hello";
  return 0;
}

Please send me a helping hand! 👍


* - Annabelle Lee - Feb 2, 2016

I used 5 seconds just to test it, but you can replace it with 30. It's based on the C++ 2011 standard, so you'll have to probably compile in regards to that:

Code
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;

int main() {
    int i;
    for(i = 0; i < 10; i++){
        cout << i << endl;
        std::this_thread::sleep_for(std::chrono::seconds(5));    
    }
    return 0;
}



* - theChive - Feb 2, 2016

(Feb 2, 2016 06:49 PM)AnnabelleLee Wrote:  I used 5 seconds just to test it, but you can replace it with 30. It's based on the C++ 2011 standard, so you'll have to probably compile in regards to that:

Spoiler (Click to View)

Thank you very much for this! I understand what you did there! 👍


* - Annabelle Lee - Feb 2, 2016

(Feb 2, 2016 07:01 PM)theChive Wrote:  Thank you very much for this! I understand what you did there! 👍

You're welcome, glad you understand it.