Thread: How do u control time in c++

  1. #1
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262

    How do u control time in c++

    Say i want to make a "loading..." appear for 5 seconds, then change to something else. How would i achieve that? Thanks
    I AM WINNER!!!1!111oneoneomne

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    #include <windows.h>
    ...
        Sleep(5000) // Pause 5,000 milliseconds
    That'll only work under Windows. The header will vary depending on your OS, but the function name and general idea will be very similar.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    For a somehwhat kludgy, yet standard way, look at the functions in the <ctime> header. They allow you to get the current time. So, if you record the starting time, then enter an infinite loop until the desired time difference is reached (by successively checking the time, perhaps not every loop iteration, though), then break out of the loop. Not terribly accurate or nice, but it should work.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Or for posix,

    #include <unistd.h>
    unsigned int sleep(unsigned int period);

    Note that it is in seconds.

    Or, for conformance to UNIX 98,

    #include <unistd.h>
    int usleep(useconds_t period);

    That's in microseconds.

  5. #5
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    Thanks
    I AM WINNER!!!1!111oneoneomne

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Zach L.
    For a somehwhat kludgy, yet standard way, look at the functions in the <ctime> header. They allow you to get the current time. So, if you record the starting time, then enter an infinite loop until the desired time difference is reached (by successively checking the time, perhaps not every loop iteration, though), then break out of the loop. Not terribly accurate or nice, but it should work.
    this is the way I would do it, but that eats up your CPU cycles like you wouldn't believe... but then again it makes it look like your program is actually doing something for those 5 seconds and not just wasting everybody's time...
    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

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    [offtopic]
    Did anyone else see the subject of the OP and giggle uncontrollably?
    Code:
    #include <ctimectrl>
    int main()
    {
        jump_ahead("5:00");
        return 0;
    }
    [/offtopic]
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by pianorain
    [offtopic]
    Did anyone else see the subject of the OP and giggle uncontrollably?
    Code:
    #include <ctimectrl>
    int main()
    {
        jump_ahead("5:00");
        return 0;
    }
    [/offtopic]
    I was thinking more along the lines of:

    Code:
    #include <iostream>
    using std::cout;
    
    int main()
    {
        cout << "The Time Machine\n";
        cout << "By H.G. Wells\n\n";
        cout << "Chapter 1\n\n";
        cout << "The Time Traveller (for so it will be convenient to speak of him) ";
        cout << "was expounding a recondite matter to us.  His grey eyes shone ";
        cout << "and twinkeled, and his usually pale face was flushed and animated.";
        etc...
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Did anyone else see the subject of the OP and giggle uncontrollably?
    I was just asking myself, "How do u control time in c++? Forget C++. How do you control time in reality?"

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by pianorain
    [offtopic]
    Did anyone else see the subject of the OP and giggle uncontrollably?
    Code:
    #include <ctimectrl>
    int main()
    {
        jump_ahead("5:00");
        return 0;
    }
    [/offtopic]
    time control wasn't part of the C standard. The timecntrl header wasn't implemented until the C142 standard came out, so it's only <timecntrl>

    don't forget that it's implementation-specific, so you don't want to get that stuck in an infinite loop...
    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

  11. #11
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation Sometimes non-portable code is better...

    but that eats up your CPU cycles like you wouldn't believe...
    RIGHT !!!

    This is an interesting case where it’s better to go with the non-standard code! Sleep(), etc., will release your CPU to do other stuff. Like major_small said, using the ISO/ANSI time functions will hog the CPU for the sole purpose of wasting time! This is BAD thing to do with a multitasking system.

    If possible, it’s better check if “loading” is actually complete than, to use a fixed 5-second delay. For example, you could use Sleep() in a loop and check it every half-second or so.

    BTW- I have a BASIC function: DelayDot(Delay, Count) that displays “Wait.......Count is the number of “dots”, and Delay is the time between “dots”. I’ve used it kinda-like a library function in several BASIC programs.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by DougDbug
    This is an interesting case where it’s better to go with the non-standard code! Sleep(), etc., will release your CPU to do other stuff. Like major_small said, using the ISO/ANSI time functions will hog the CPU for the sole purpose of wasting time! This is BAD thing to do with a multitasking system.
    d00der... in this case it's not bad... if you want to make it look like your program's doing something, this is a good thing... and it's not the functions that are doing it, it's your semi-infinite loop that's eating the cycles... find a way to slow the loop and you've got a way to lower the amount of CPU cycles you're using up...

    Sleep() actually puts your program's thread into a sleep mode, where it can't do anything... but using the method I said, you can have your program "do that for 5 minutes"...
    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

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The Boost.Thread library provides a sleep function that works on multiple platforms.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    if you want to make it look like your program's doing something, this is a good thing...
    You can update a progress bar several times a second if you wish. You don't need to use-up all of the CPU cycles to do this.

    find a way to slow the loop and you've got a way to lower the amount of CPU cycles you're using up...
    Ummm... like Sleep() ?

    OK, it's not such a terrible thing for a user-mode program to waste a few (or a few billion) cycles for 5 seconds once in a while. The operating system will interrupt the time-wasting loop as it allocates time to other applications & processes. And, most of the time there's nothing else for the processor to do anyway. It's just one of those "what if everybody did it?" situations... If you were running several applications, and they are all running lots of tight "do-nothing" loops, you'd notice it.

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>If you were running several applications, and they are all running lots of tight "do-nothing" loops, you'd notice it.
    I notice it when I have an un-Sleep(1)'ed PeekMessage() loop running, and I'm trying to compile a second program that's supposed to interact with it i.e. the second program takes about 20 times as long to compile, everything else on the computer slows to a standstill including switching apps etc..

    Moral of the story: Don't deadsleep your programs for extended periods of time
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. last write time
    By pastitprogram in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2008, 06:54 PM
  2. (Multiline) Edit Control Limit
    By P4R4N01D in forum Windows Programming
    Replies: 9
    Last Post: 05-17-2008, 11:56 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 07:11 AM
  5. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM