Timers

This is a discussion on Timers within the C++ Programming forums, part of the General Programming Boards category; i want to define a 5 secound period where the user will input numerical data from the keypad.while that it ...

  1. #1
    Registered User Ajsan's Avatar
    Join Date
    Dec 2003
    Posts
    55

    Timers

    i want to define a 5 secound period where the user will input numerical data from the keypad.while that it happening i want the timer on the screen to count down in tenths of secounds and when it reched an incorrect key stroke or the end of time or the max input it stops and calls functions bases on the numberical enterd data

    how can i get the timer to count down?

    i know kbhit and getch so that isn't a problem?

    how do i structer that that for/while loop to accomplish the above?

    any suggestion and answers are apreciated.
    Style is overrated.

    - ₽ -

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    The answer is system and compiler dependant. Whatcha using?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User Ajsan's Avatar
    Join Date
    Dec 2003
    Posts
    55
    i'm using VC++ 6.0 Pro and if it matters Windows 9X.
    Style is overrated.

    - ₽ -

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Q&D hackery
    Code:
    for ( timer = 50 ; timer >= 0 ; timer-- ) {
      // print decreasing amount of time
      if ( kbhit() ) {
        // read one key
        // check for completion
        // break on completion
      }
      sleep for 1/10th second
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Just make a fast loop that doesn't "do anything" until an "important event" happens.

    Every time through the loop, check to see how much time has elapsed. It it time to update the tenths-of-a-second display? And, check to see if the user entered anything. Did the user make the correct entry?

    I'd probably use the timer as my main end-loop condition:
    i.e. while(Time < 5 sec)
    Then "break;" depending on the user's input.

    When you get it working, you should probably experiment with some delay/pause in the loop... maybe a few milliseconds... to free-up some CPU cycles and allow your multitasking operating system to do something else. (Actually, your program can get interrupted by the OS even if you don't pause.) Short-duration timing is always tricky with a multitasking OS.

  6. #6
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    if you writing a windows application you can use the win32 api function GetTickCount() function it returns the number of milliseconds

    Code:
    double timer=5;
    
       DWORD starttime=GetTickCount();
       
       //do code or whatever
    
       if((GetTickCount()-starttime<100)   //difference in milliseconds
         {
           timer-=.1;
          }
    if you are using a winproc to handle timer messages you can use
    SetTimer();
    Code:
    in WM_CREATE maybe do this
    
    SetTimer(hwnd,TIMER_ID_1TENTH,100,NULL);
    
    and in WIN_PROC
    case WM_TIMER:
            {
                switch(wparam)
                 {
                       case TIMER_ID_1TENTH:
                                //do processing here
                          break;
                   }
    "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/

  7. #7
    Registered User Ajsan's Avatar
    Join Date
    Dec 2003
    Posts
    55
    Thank you all for the help. i have realized that i'm an idiot and could have figuired it out if i had taken about another minute to think about it.
    Style is overrated.

    - ₽ -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 07:56 AM
  2. Enumerating timers and hotkeys
    By maxorator in forum Windows Programming
    Replies: 3
    Last Post: 01-01-2007, 10:47 AM
  3. switch cases and timers
    By soranz in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2005, 06:43 PM
  4. Timers, Timers, Timers!
    By Stan100 in forum Game Programming
    Replies: 9
    Last Post: 01-24-2003, 03:45 PM
  5. Timers
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2001, 03:34 AM

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