Sign in to follow this  
Azurecat

Very weird timer issue

Recommended Posts

Azurecat    103
[size=4][font=arial,helvetica,sans-serif][color=#000000]I'm trying to make a countdown timer for my program that warns the player whenever x seconds are left (60, 30, 10, etc.)[/color][/font][/size]

[size=4][font=arial,helvetica,sans-serif][color=#000000]I got it working, but it only works if I print the time remaining on each cycle.[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000]Here's my code so far (not the entire program):[/color][/font][/size]

[size=4][font=arial,helvetica,sans-serif][color=#000000]**************************************************************************************************************************[/color][/font][/size]


[size=4][font=arial,helvetica,sans-serif][color=#000000]void Delay(int seconds)[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000]{[/color][/font][/size]
[color=#7041a7][size=4][font=arial,helvetica,sans-serif][color=#000000] clock_t t;[/color][/font][/size][/color]
[size=4][font=arial,helvetica,sans-serif][color=#000000] t = clock() + (seconds * CLOCKS_PER_SEC);[/color][/font][/size]

[size=4][font=arial,helvetica,sans-serif][color=#000000] while (clock() < t)[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000] {[/color][/font][/size]

[size=4][font=arial,helvetica,sans-serif][color=#000000] }[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000]}[/color][/font][/size]

[size=4][font=arial,helvetica,sans-serif][color=#000000]void StartTimer(int seconds)[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000]{[/color][/font][/size]

[size=4][font=arial,helvetica,sans-serif][color=#000000] for (int i = 0; i < seconds; i++)[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000] {[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000] int remaining = seconds - i;[/color][/font][/size]

[size=4][font=arial,helvetica,sans-serif][color=#000000] if (remaining == 60)[/color][/font][/size]
[color=#cf3125][size=4][font=arial,helvetica,sans-serif][color=#000000] cout << "\n60 seconds remaining";[/color][/font][/size][/color]

[size=4][font=arial,helvetica,sans-serif][color=#000000] else if (remaining == 45)[/color][/font][/size]
[color=#cf3125][size=4][font=arial,helvetica,sans-serif][color=#000000] cout << "\n45 seconds remaining";[/color][/font][/size][/color]

[size=4][font=arial,helvetica,sans-serif][color=#000000] else if (remaining == 30)[/color][/font][/size]
[color=#cf3125][size=4][font=arial,helvetica,sans-serif][color=#000000] cout << "\n30 seconds remaining";[/color][/font][/size][/color]

[size=4][font=arial,helvetica,sans-serif][color=#000000] else if (remaining == 20)[/color][/font][/size]
[color=#cf3125][size=4][font=arial,helvetica,sans-serif][color=#000000] cout << "\n20 seconds remaining";[/color][/font][/size][/color]

[size=4][font=arial,helvetica,sans-serif][color=#000000] else if (remaining == 10)[/color][/font][/size]
[color=#cf3125][size=4][font=arial,helvetica,sans-serif][color=#000000] cout << "\n10 seconds remaining";[/color][/font][/size][/color]

[size=4][font=arial,helvetica,sans-serif][color=#000000] cout << remaining << "\n"; //for some reason nothing prints at the[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000] //right time unless I include this line.[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000] Delay(1);[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000] }[/color][/font][/size]

[size=4][font=arial,helvetica,sans-serif][color=#000000]}[/color][/font][/size]

[size=4][font=arial, helvetica, sans-serif][color=#000000]I've tried "dummy" variables, an empty "else" block, moving things around, but nothing seems to work. I'm guessing it's either:[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000]A. bad use of "if" statements[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000]B. somewhere the << is overloaded in a way I don't know about.[/color][/font][/size]
[size=4][font=arial,helvetica,sans-serif][color=#000000]I have a really hard time believing it could be B, but it's happened before...[/color][/font][/size]

[size=4][font=arial,helvetica,sans-serif][color=#000000]Anybody have any ideas on what I'm missing?[/color][/font][/size]

Share this post


Link to post
Share on other sites
Dragonion    131
The following works fine on my system:

[code]#include <ctime>
#include <iostream>

using namespace std;

void Delay (int seconds)
{
clock_t t = clock() + (seconds * CLOCKS_PER_SEC);

while (clock() < t) ; // this semi-colon has the exact same effect as { }
}

void StartTimer(int seconds)
{
for (int i = 0; i < seconds; i++)
{
int remaining = seconds - i;

if (remaining == 60)
cout << "\n60 seconds remaining";

else if (remaining == 45)
cout << "\n45 seconds remaining";

else if (remaining == 30)
cout << "\n30 seconds remaining";

else if (remaining == 20)
cout << "\n20 seconds remaining";

else if (remaining == 10)
cout << "\n10 seconds remaining";

// cout << remaining << "\n"; //for some reason nothing prints at the
// right time unless I include this line.

Delay(1);
}
}

void main (void)
{
StartTimer(60);
}[/code]

However, may I recommend a switch statement instead:

[code]void StartTimer(int seconds)
{
for (int i = 0; i < seconds; i++)
{
switch(seconds-i)
{
case(60):
cout << "\n60 seconds remaining";break;
case(45):
cout << "\n45 seconds remaining";break;
case(30):
cout << "\n30 seconds remaining";break;
case(20):
cout << "\n20 seconds remaining";break;
case(10):
cout << "\n10 seconds remaining";break;
}
Delay(1);
}
}[/code]

... or even more compact:

[code]void StartTimer(int seconds)
{
for (int i = 0; i < seconds; i++)
{
switch(seconds-i)
{
case(60):
case(45):
case(30):
case(20):
case(10):
cout << "\n" << int(seconds-i) << " seconds remaining";
}

Delay(1);
}
}[/code]

Share this post


Link to post
Share on other sites
Azurecat    103
Thanks for the help. Sorry it took so long to get back (school is super busy this quarter).

@ Telastyn: I'm calling Delay(int seconds) in the for loop, so the loop should run once every second, so I'm not really sure what you mean.

@ Dragonion: I wonder why it runs fine on yours? I cleared all my other code in main() and just called StartTimer(). When the 60 second mark comes around it just prints a blank new line, then prints 60 at the 45 second mark, 45 at the 30, and so on. Even if I call StartTimer(35) it just prints a newline at the first "mark" and is always behind in the display. The counting is just fine, but it prints late (10 at 0 is kind of misleading to the player). Any thoughts on what might be causing this?

By the way, thanks for showing the shorter switch statement. I've used them before but never that concisely :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this