1

All the 10 questions with 5 marks need to be answered within time. so the time consumed for each question n remaining time should be displayed. can anybody help?

Driftr
  • 11
  • 1
  • 2
  • From your comment below I suggest that you be more specific about how you want your timer to "enforce" the program flow. It's simple enough to just check afterwards if the answer was within the time limit, but if you want to interrupt the program flow based on a timer, you have to be more sophisticated. – Kerrek SB Aug 14 '11 at 12:25

2 Answers2

4

A portable C++ solution would be to use chrono::steady_clock to measure time. This is available in C++11 in the header <chrono>, but may well be available to older compilers in TR1 in <tr1/chrono> or boost.chrono.

The steady clock always advances at a rate "as uniform as possible", which is an important consideration on a multi-tasking multi-threaded platform. The steady clock is also independent of any sort of "wall clock", like the system clock (which may be arbitrarily manipulated at any time).

(Note: if steady_clock isn't in your implementation, look for monotonic_clock.)


The <chrono> types are a bit fiddly to use, so here is a sample piece of code that returns a steady timestamp (or rather, a timestamp from whichever clock you like, e.g. the high_resolution_clock):

template <typename Clock>
long long int clockTick(int multiple = 1000)
{
  typedef typename Clock::period period;
  return (Clock::now().time_since_epoch().count() * period::num * multiple) / period::den;
}

typedef std::chrono::monotonic_clock myclock;  // old
typedef std::chrono::steady_clock yourclock;   // C++11

Usage:

long long int timestamp_ms = clockTick<myclock>();         // milliseconds by default
long long int timestamp_s  = clockTick<yourclock>(1);      // seconds
long long int timestamp_us = clockTick<myclock>(1000000);  // microseconds
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • was never part of TR1. Also I think it's better to use the clock's time_point and duration types for the type safety they provide. It's harder to get confused as to what's a point in time vs. a duration, and what units, nanoseconds or whatever, are being used that way. – bames53 Sep 20 '11 at 15:34
2

Use time().

This has the limitation that Kerrek has pointed out in his answer. But it's also very simple to use.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • "-1" - that's immediately vulnerable to global clock manipulations, even by other processes. Preferably use the steady clock from `` or ``. OP is asking for a *duration*, not a *timestamp*. – Kerrek SB Aug 14 '11 at 11:51
  • @Kerrek: That's C++0x-specific, no? – Oliver Charlesworth Aug 14 '11 at 11:54
  • Oli: Well, it's in TR1 and in Boost, so there are several ways to get at it. – Kerrek SB Aug 14 '11 at 11:56
  • @Kerrek: Fair enough. Possibly slightly overkill for something this simple, but good advice in general, I guess. +1 for your answer. – Oliver Charlesworth Aug 14 '11 at 11:57
  • Hehe, thanks - you think the answer is overkill, wait till you see the verbose code necessary to *read* values from the steady clock ;-) – Kerrek SB Aug 14 '11 at 12:00
  • I should have 60 seconds initially n it should decrease with system time as time taken for each question may vary! – Driftr Aug 14 '11 at 12:00
  • 1
    @rajikumar: You just record the clock value at the start, and then again after the answer comes in, and subtract the duration from the global counter. If you want to be actually _interrupted_ if you take too long, you'll have to implement something with callbacks or timeouts (check out `libevent`, or a `select()`-based read from the input, neither are standard C++). – Kerrek SB Aug 14 '11 at 12:02
  • can i do something easily using vc++? – Driftr Aug 14 '11 at 12:04
  • The OP didn't say it had to be proof against cheating. Downvoting an answer because the suggested approach can be manipulated seems a bit harsh. :) – jalf Aug 14 '11 at 12:16
  • 1
    @Jalf: I didn't actually downvote, just in quotation marks :-) Oli's answer is fine for a proof-of-concept. Just don't use it in a voting machine or an airport security scan, or when paying out winnings on your iPhone gambling app :-) – Kerrek SB Aug 14 '11 at 12:17
  • @rajkumar: Kerrek already suggested how to do this. Which part are you having trouble with? – Oliver Charlesworth Aug 14 '11 at 12:40
  • @Kerrek SB In all those situations I do really hope that your average user doesn't have access to the system where the time is measured ;) Since this is done on the users own system, you've lost anyhow - nothing is stopping him or her from overwriting your call with something else or using a debugger to change values or whatnot. Sure that's more complicated to do so, but you can't make it tamperproof anyhow and time is simple and always available - that's surely something worth too. – Voo Aug 14 '11 at 15:23
  • @Voo: What about daylight savings time? Or just NTP? Even if the unprivileged user is accessing the quiz through a web frontend, a wall clock is not the most reliable tool for duration measurement... – Kerrek SB Aug 14 '11 at 15:26
  • @Kerrek SB Sure it depends on what exactly you want, nobody is saying that you can't trick clock(). Note that the same problems you get with NTP (ie os slowing down the tick rate if local time is too fast) may also happen with chrono and co - there's nothing that can guarantee perfectly uniform ticks. It all depends on what you're after - if one is aware of and fine with the limitations of clock() you can't get a simpler solution that also doesn't require external dependencies on any normal system (VC++ doesn't have chrono) – Voo Aug 15 '11 at 01:40
  • i'm just new to programming! i'm not able to get your high thoughts which am not aware of! so can someone paste a easy code for me? thanks in advance – Driftr Aug 15 '11 at 08:49