2

I'm a C++ beginner. I'm searching for a way to may a simple timer function for a very simple console game (class assignment). Hopefully a preexisting function from a common library? Please keep it simple since I am new. Thanks. Sorry if I'm not wording this correctly. Brand new to this forum as well!

dana_muise
  • 41
  • 1
  • 4
  • 9
  • you can learn from this thread http://stackoverflow.com/questions/3220477/how-to-use-clock-in-c – Ricky Apr 01 '13 at 06:24
  • 4
    OT : SO is not a "forum". – Daniel Kamil Kozar Apr 01 '13 at 06:27
  • 5
    @DanielKamilKozar and its upvoter: I preatty don't understand what is OT here. It is a simple question, but still a question, that admit answers. It if is too simple for you just don't aswer. Slamming up the door to a newcomer doesn't make we better. – Emilio Garavaglia Apr 01 '13 at 06:32

3 Answers3

1

http://en.cppreference.com/w/cpp/chrono/c/time

http://www.cplusplus.com/reference/ctime/

It's not that different from any other loop or iteration, simply use the given values/constant as your condition.

pgf
  • 65
  • 1
  • 6
0

you can use the time function from #include <time.h>. Use it to get a start and stop time, then subtract to get the elapsed time. Info here.

d.moncada
  • 16,900
  • 5
  • 53
  • 82
0

Assuming you are using plain C++ in Win32 there is a function SetTimer() from the Win32 API that goes like this

UINT myTimer = SetTimer ( hwnd, ID_60SECONDS, 60 * 1000, NULL );

You will then need to add a case statement to your WndProc to handle the elapsed timer

case WM_TIMER:
switch (wParam)

{

    case ID_60SECONDS:

    // We processed it, so return 0

    return 0;

}

break;

Unfortunately, this involves dealing with Win32 which is no that easy but once you got the hang it is very obvious.

I took the code from this tutorial.

bash.d
  • 13,029
  • 3
  • 29
  • 42