Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

[C++] Making a timed loop

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! 👍
Keep calm, chive on.
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;
}
(Feb 2, 2016 06:49 PM)Annabelle Lee 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! 👍
Keep calm, chive on.
(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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by ohxerxeFeb 4, 2018 03:26 PM 3 304 Feb 4, 2018 03:26 PM
Last Post: matt
[Java] by prodigyhfSep 1, 2017 05:22 PM 0 221 Sep 1, 2017 05:22 PM
Last Post: prodigyhf
[.NET] by KingHankApr 10, 2015 02:43 PM 6 135 Apr 10, 2015 02:43 PM
Last Post: Nikey646