Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7998403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:12:22+00:00 2026-06-04T15:12:22+00:00

How to load a loop every 10 second and add +1 to the count

  • 0

How to load a loop every 10 second and add +1 to the count and print it?

like:

int count;
    while(true)
    {
       count +=1;
       cout << count << endl; // print every 10 second 
    }

print:

1
2
3
4
5
ect...

i dont know how, please help me out guys

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:12:22+00:00Added an answer on June 4, 2026 at 3:12 pm

    My try. (Almost) perfectly POSIX. Works on both POSIX and MSVC/Win32 also.

    #include <stdio.h>
    #include <time.h>
    
    const int NUM_SECONDS = 10;
    
    int main()
    {
        int count = 1;
    
        double time_counter = 0;
    
        clock_t this_time = clock();
        clock_t last_time = this_time;
    
        printf("Gran = %ld\n", NUM_SECONDS * CLOCKS_PER_SEC);
    
        while(true)
        {
            this_time = clock();
    
            time_counter += (double)(this_time - last_time);
    
            last_time = this_time;
    
            if(time_counter > (double)(NUM_SECONDS * CLOCKS_PER_SEC))
            {
                time_counter -= (double)(NUM_SECONDS * CLOCKS_PER_SEC);
                printf("%d\n", count);
                count++;
            }
    
            printf("DebugTime = %f\n", time_counter);
        }
    
        return 0;
    }
    

    This way you can also have the control on each iteration, unlike the sleep()-based approach.

    This solution (or the same based on high-precision timer) also ensures that there is no error accumulation in timing.

    EDIT: OSX stuff, if all else fails

    #include <unistd.h>
    #include <stdio.h>
    
    const int NUM_SECONDS = 10;
    
    int main()
    {
        int i;
        int count = 1;
        for(;;)
        {
            // delay for 10 seconds
            for(i = 0 ; i < NUM_SECONDS ; i++) { usleep(1000 * 1000); }
            // print
            printf("%d\n", count++);
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I load content via Ajax and then I'll have element like $('.my_content_class') and I
I load a page with jquery like this: $('#container').load('books/book1/inleiding.php'); However, the images are in,
How would i loop every 5 seconds the following code : sendSocket = socket.socket
I would like my MVC application to check (on every request) if the current
i have a exrate.xml look like this <!--For reference only. Only one request every
I have valid data in jquery, how to I loop every item I get
I'm writing a quick little add-in for Outlook that will check my email every
Every time the program terminates when it runs the third time through this loop:
load() just returns a proxy by default and database won’t be hit until the
ViewA load some data from Coredata, then push to the next viewB . In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.