time.h powered function acting strange

I am trying to learn to use time.h to make a simple program event based. I made this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void wait()
{
    time_t start = time(NULL);
    time_t end = time(NULL) + 10;
    bool stop = false;
    cout << "start = " << start << " end = " << end << endl;
    while(!stop)
    {
        if(start < end)
            cout << time(NULL) << endl;
        if(end >= end)
            stop = true;
    }
}


but it just keeps displaying the iterations of time in an unendung loop.

What I was trying for was a display of the time repeating until end was reached.
end >= end will always be true so the loop will end right away.

What you probably want is time() >= end.
Last edited on
Doh!
Thank you.
P.S. I had to use time(NULL) >= end

P.S.S. What would be the simplest and easiest way to do something in the program in 1 second intervals rather than the hundreds or thousands a modern computer is capable of? The output fills the screen fast.
ahaha, I just read this and coded it, because I've never used time_t before...

Here's the code:
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
48
#include <iostream>		//cout
#include <conio.h>		// _kbhit()
#include <time.h>		//time_t

void update();

int main()
{
	time_t start = time( NULL );

	//I've only done a typecast to get-rid of the warning:
	// conversion from 'double' to 'time_t', possible loss of data
	//I only get this warning because I'm using .5 rather than a whole number
	time_t interval = start + ( time_t ).5;

	std::cout << "Start:\t\t\t" << start << "\nUpdate interval:\t" << interval << '\n';

	while( ! _kbhit() )
	{
		//keep updating the time each loop
		start = time( NULL );

		//when the time is greater than the desired interval
		if( start > interval )
		{
			//reset the interval to the current time, + desired interval
			interval = start + ( time_t ).5;

			//call an update function to run code
			update();
		}
	}


	//press a key to continue...
	std::cout << "\n\nPress any key to continue...";

	//while there isn't a key being pressed
	while( ! _kbhit() )
	{ /* do nothing - wait for a key press, before exiting */ }

	return 0;
}

void update()
{
	std::cout << "Updating...\n";
}


Edit:
Edited the code, added comments etc.
Last edited on
Here's my new function:
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
void wait()
{
    time_t start = time(NULL);
    time_t end = time(NULL) + 10;
    bool stop = false;
    int interval = 0;
    int next = 0;
    cout << "start = " << start << " end = " << end << endl;
    while(!stop)
    {
        interval = time(NULL);
        if(time(NULL) < end)
        {
            if(interval != next)
            {
                cout << time(NULL) << endl;
                next = time(NULL);
            }
        }
        if(time(NULL) >= end)
        {
            stop = true;
        }
    }
}

it finally does what I want it to.
Lynx876, I had a difficult time determining what you were trying to say. It looked like a complete re-write of all my code. A little confusing. Thanks for the feedback, tough.
I still feel like I should be doing this a different way. But aside from the obvious disadvantages of using cout rather than something from conio.h, I am definitely getting a nagging feeling that my if statements are really confusing things for any one looking at my code.

edit:
I also discovered that if start = 1 and end = 10, it will display 1,2,3,4,5,6,7,8,9 but never 10. I'm pulling my hair out. (><)
Last edited on
Yeah, I kind of only glanced at your code and saw the time_t and deciced to try it for myself. I actually wrote that before you posted, asking about 1 second intervals.

The code I wrote will update every .5, which I thought was going to be a half a second. But if you run that code, and put the console near the clock on your computer, and open the clock. You 'may' noticed that it's about as close to a second as can get, without using more decimal places. Though, I'm not sure if this would be CPU specific.

If this is not CPU specific, time_t end = time(NULL) + 10; would be a 20 second wait, with no way to exit your loop.

Lines 14 -18 will never get executed within your code.
You initialize interval and next to 0
Then change interval to time( NULL )
But never change next.

Thus, interval will always be greater than next!

And as you said, I think your code is confusing! More than it has to be.

Read through my above while loop. You just need to keep updating the time until the interval has been reached. At that point, recreate an interval, which is the current time + the time in which you require another update.

Edit:
An edit for your edit... lol

if(time(NULL) < end)

Here, end is equal to the start time + 10. So here, time would be 10 and so would the end time
10 cannot be less that 10!
Last edited on
If this is not CPU specific, time_t end = time(NULL) + 10; would be a 20 second wait, with no way to exit your loop.

It should be dependent on the actual time and not the cpu. i have researched this in depth.

...with no way to exit your loop.

I an add this functionality later if needed.

Lines 14 -18 will never get executed within your code.

I hate to seem like I am arguing with some one whom knows better than me but I took out lines 14-18 and it changed the displaying portion of the count but still counted 10 seconds and then ended the program.

And as you said, I think your code is confusing! More than it has to be.

I still agree with you on this, however, I will address that when I have figured out how to get the exact result I am looking for. I have absolute confidence that I can clean it up later.

Again, thank you for your feedback. I am sure, with your continued help, I can solve my dilemma.
Lines 14 -18 will never get executed within your code.

I hate to seem like I am arguing with some one whom knows better than me but I took out lines 14-18 and it changed the displaying portion of the count but still counted 10 seconds and then ended the program.
I was meant to take that out! I wrote that, then read the code 3-4 times and then saw what you were doing, lol.

I hate to seem like I am arguing with some one whom knows better...
It's never a case of arguing, I can almost garentee, you know something I don't! Never think someone is right until you've tested, understood it and had it confirmed they are right ;) ahaha. I never claim to be right, only try to help and steer in the right direction. I've been corrected more than once on this forum, but still try to help when I have input / a theory.

As for #include <conio.h> , I'm only using that for _kbhit() so I can pause the program before exiting, so the user can see the output program.

If you run my above code, without the last while-loop and remove the update function, what you'll have left it a 'timer' that runs an update every second( or close enough ).
Topic archived. No new replies allowed.