Thread: timer

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    102

    timer

    hello, i know this has been posted many times, and i have read all of the other threads.
    what i want to do is count down in seconds from 10 to 1 and display it on screen. (just as an example)
    But, i dont want to use the sleep function, because its not portable.
    i have recently read somewhere how to do it. But i cant remember where!

    please help.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    This thread covers most all your options.

    gg

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    looks like i will have to use sleep() then, as for the portable solution kills the computer.

    but when i put the code:

    Code:
    #include <iostream.h>
    #include <windows.h>
    
    int main()
    {
    for(int c=0;c<10;c++)
    {
    	cout<<c;
    	Sleep(10);
    }
    return 0;
    }
    it just waits for 10 miliseconds an then writes 123456789 all at the same time, instead of waiting 10 miliseconds each time through the loop?

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    10 milliseconds is one hundreth of a second, not a long time! Try a larger value like a thousand(one second).

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    yes i know, if i put 1000 in it waits 10 seconds and then prints 123456789 all at the same time. i dont know why?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >and then prints 123456789 all at the same time
    Flush the buffer after each character:
    Code:
    cout<<c<<flush;
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    cheers, works great.
    what exactly does 'flush' do?
    does it just empty the contents of a variable?
    Last edited by b00l34n; 04-05-2004 at 02:05 PM.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what exactly does 'flush' do?
    It forces the output to be written to a device (in this case the screen) immediately. Otherwise your program would wait to flush the buffer until it was full, or something else caused a flush (such as endl) for performance reasons.
    My best code is written with the delete key.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    something I've been wondering: how do you know how big the buffer is (just curious)
    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. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 12:23 AM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Need help with a count down timer
    By GUIPenguin in forum C# Programming
    Replies: 0
    Last Post: 07-07-2006, 04:18 PM
  4. Timer again.
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 05-04-2005, 10:19 PM