How do I create a count down timer for 1hr 30mins

I want to add a count down timer to my testing software 1hr and 30min

Its a 32bit console application, I want the countdown timer outputted above each question on the right side, timer only needs to output the time left at the top of each question so a live outputted timer is not important, but I would be very grateful if someone could help me use either.

Questions are not random generated.

Also if somebody could help me to output "out of time" if the timer reaches 0
and for the test to end, I think branching is involved not too sure.

I already have, std::srand( std::time(0) ) ; at the start of the test to random
generate the answer order.

Thanks.
Last edited on
if the operating system is windows then use system timers http://msdn.microsoft.com/en-us/library/windows/desktop/ms632592%28v=vs.85%29.aspx
cout <<" " << endl ;
Last edited on
You dont need gui to use windows API...
Since it doesn't look like you need a high resolution timer, you can probably get away with the facilities provided by the standard C library time.
http://www.cplusplus.com/reference/ctime/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <limits>
#include <ctime> // time_t, difftime, time

int main ()
{
    //we just started the program, so let's take a snapshot of the time
    std::time_t startTime = std::time(nullptr);

    //we'll use this time_t variable to take future snapshots of the time to compare them
    //to when we started
    std::time_t currentTime;
    double seconds;

    std::cout << "Input something...";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::time(&currentTime); //get the time now that the user has entered something and stick it in currentTime

    seconds = std::difftime(currentTime, startTime);
    std::cout << "It took you " << seconds << " seconds to enter something.\n";

    std::cout << "Input something else...";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::time(&currentTime);

    seconds = std::difftime(currentTime, startTime);
    std::cout << "It has now been " << seconds << " seconds since you started the program.\n";

    return 0;
}
Last edited on
I can't remember ever seeing NULL in a 32bit console application code.

Then you clearly have never read the example code on MSDN. "NULL" is a macro for '0', it is defined in 'WinDef.h' which is a header that gets included into your code throught 'Windows.h'.

@ Computergeek01 I'm not familiar with it probably because I'm not programming too long or enough. In fact I did read that page but because I'm fairly new I'm not sure how to implement this code into my 32 bit console app, and I tried to program with a GUI before (didn't go well) I remember using some nulls to open a message box (can't remember) But I never needed to use them in console app so maybe my comment sounded ignorant which was not meant to be.

Maybe you could explain how to implement this code (SetTimer function) into a 32bit console app ?

UINT_PTR WINAPI SetTimer(
_In_opt_ HWND hWnd,
_In_ UINT_PTR nIDEvent,
_In_ UINT uElapse,
_In_opt_ TIMERPROC lpTimerFunc
);
Last edited on
@ Tath ok thanks for the advice I know better now, just not sure how to implement it.
Last edited on
@ booradley60 Thanks a lot I will try this and let you know the outcome.
@ booradley60

I'm getting this error when I try to use the code.


9:39: error: 'nullptr' was not declared in this scope ?
Last edited on
here you have it explained http://stackoverflow.com/questions/7531650/can-i-use-a-settimer-api-in-a-console-c-app

but to make it do something useful you would still need to make your application multi threaded

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#define STRICT 1 
#include <windows.h>
#include <iostream>

using namespace std;

VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime) {
  cout << "Time: " << dwTime << '\n';
  cout.flush();
}

int main(int argc, char *argv[], char *envp[]) {
      int Counter=0;
      MSG Msg;
      UINT TimerId = SetTimer(NULL, 0, 500, &TimerProc);

      cout << "TimerId: " << TimerId << '\n';
      if (!TimerId)
        return 16;
      while (GetMessage(&Msg, NULL, 0, 0)) {
        ++Counter;
      if (Msg.message == WM_TIMER)
        cout << "Counter: " << Counter << "; timer message\n";
      else
        cout << "Counter: " << Counter << "; message: " << Msg.message << '\n';
      DispatchMessage(&Msg);
    }

    KillTimer(NULL, TimerId);

    return 0;
}
Topic archived. No new replies allowed.