timer problems

I'm looking to create a timer that counts down from a random amount of time.
Basically the timer should be set to a random amount of time in a certain time frame, and count down from that time to zero. I've been trying to understand how all the things in the time.h work, but it doesn't stick in my head. Here's what I have so far:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <time.h>



class Timer
{
    public:
        clock_t max_val;
        clock_t min_val;
        Timer();
        unsigned long int determine_start_val(clock_t max, clock_t min);
        unsigned long int start_val;
        clock_t countdown();

};

Timer::Timer()
{
    clock_t max_val = clock() / CLOCKS_PER_SEC * 15;
    clock_t min_val = clock() / CLOCKS_PER_SEC * 5;
}

unsigned long int Timer::determine_start_val(clock_t max, clock_t min)
{
    time_t now;
    time(&now);
    srand(now);
    unsigned long int start_time = rand();

    while (start_time >= max || start_time <= min)
    {
        start_time = 0;
        start_time = rand();
    }

    return start_time;
}

clock_t Timer::countdown()
{
    clock_t running_time;
    running_time = clock();

    unsigned long int total_time = (clock() - running_time) / CLOCKS_PER_SEC;

    return total_time;
}


What's wrong with this?
It would help if you gave an example of how this was to be used. The interface isn't intuitive to me.

What are your trying to accomplish in Timer::countdown()? The total_time ( the value returned) is almost always going to be zero because the amount of time from setting running_time to setting total_time will be measured in nanoseconds (significantly less that CLOCKS_PER_SEC).
Basically I need it to set a random time and count down from that time until 0. At 0, I need the program to exit.
Do you need to count in seconds? If so, just iterate downwards through a loop, using sleep(1) each iteration, which will cause your application to sleep for a second.

Here is a sample application with comments which will hopefully help you understand:

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
#include <iostream>
#include <cassert>
#include <ctime>

int main()
{
    int min;
    std::cout << "enter min time value (in s): ";
    std::cin >> min;

    int max;
    std::cout << "enter max time value (in s): ";
    std::cin >> max;

    assert(max > min);

    // seed random number generator with current time
    srand(time(NULL));
    // get a random number, and ensure it fits within the specified time window
    int num = rand() % (max - min) + min;

    // count down to zero, displaying the time left and then sleeping for a second
    while (num > 0)
    {
        std::cout << num-- << std::endl;
        sleep(1);
    }
    return 0;
}
I'll try to run that. I just have a little bit of difficulty understanding why we take the modulus of the division of a random number by (max - min) and then add it to min.
Topic archived. No new replies allowed.