How to let the second run?

This is a discussion on How to let the second run? within the C++ Programming forums, part of the General Programming Boards category; How to let the second run (that mean always change the number of second) when run the program? Code: time_t ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    7

    How to let the second run?

    How to let the second run (that mean always change the number of second) when run the program?


    Code:
    time_t today;	
    time(&today);
    struct tm*date=localtime(&today);
    int d=date->tm_mday;
    int m=date->tm_mon+1;
    int y=date->tm_year+1900;
    int h=date->tm_hour;
    int t=date->tm_min;
    int s=date->tm_sec;
    cout<<"\nDate: "<<d<<"/"<<m<<"/"<<y;
    cout<<"\t\tTime: "<<h<<":"<<t<<":"<<s;
    Last edited by nasa; 03-18-2004 at 05:52 AM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You mean constantly update the seconds?
    Code:
    while(Running)
    {
      Seconds++; //Increase seconds
      std::cout << Seconds; //Print seconds
      sleep(1); //Sleep 1 second until next update
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you could also do it this way if you don't want to use the Sleep() function...
    Code:
    while(running)
    {
         timer=clock();
         if(timer%1000==0)
              std::cout<<timer<<" "<<std::flush;
    }
    by the way, sleep has a capital 'S'
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If I'm not entirely mistaken, sleep(t) (tiny s) sleeps for t seconds, while Sleep(t) (capital S) sleeps for r milliseconds. The latter requires windows.h.

    sleep actually sleeps the calling thread, while your solution still puts a heavy load on the CPU though a death loop.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, your right... I forgot about the sleep with a lowercase 's'... and my method is very CPU intensive, but I don't know about the portability of the sleep code...

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  2. how to run an exe command in c++ and get back the results?
    By mitilkhatoon in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2006, 06:00 PM
  3. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 03:04 AM
  4. How I can Run exe file in C++
    By palang in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2006, 11:55 AM
  5. Replies: 2
    Last Post: 10-29-2002, 03:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21