Very weird timer issue

Started by
2 comments, last by Azurecat 12 years ago

[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.)[/font]

[font=arial,helvetica,sans-serif][color=#000000]I got it working, but it only works if I print the time remaining on each cycle.[/font]

[font=arial,helvetica,sans-serif][color=#000000]Here's my code so far (not the entire program):[/font]

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


[font=arial,helvetica,sans-serif][color=#000000]void Delay(int seconds)[/font]

[font=arial,helvetica,sans-serif][color=#000000]{[/font]
[color=#7041a7]

[font=arial,helvetica,sans-serif][color=#000000] clock_t t;[/font]

[font=arial,helvetica,sans-serif][color=#000000] t = clock() + (seconds * CLOCKS_PER_SEC);[/font]

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

[font=arial,helvetica,sans-serif][color=#000000] {[/font]

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

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

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

[font=arial,helvetica,sans-serif][color=#000000]{[/font]

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

[font=arial,helvetica,sans-serif][color=#000000] {[/font]

[font=arial,helvetica,sans-serif][color=#000000] int remaining = seconds - i;[/font]

[font=arial,helvetica,sans-serif][color=#000000] if (remaining == 60)[/font]
[color=#cf3125]

[font=arial,helvetica,sans-serif][color=#000000] cout << "\n60 seconds remaining";[/font]

[font=arial,helvetica,sans-serif][color=#000000] else if (remaining == 45)[/font]
[color=#cf3125]

[font=arial,helvetica,sans-serif][color=#000000] cout << "\n45 seconds remaining";[/font]

[font=arial,helvetica,sans-serif][color=#000000] else if (remaining == 30)[/font]
[color=#cf3125]

[font=arial,helvetica,sans-serif][color=#000000] cout << "\n30 seconds remaining";[/font]

[font=arial,helvetica,sans-serif][color=#000000] else if (remaining == 20)[/font]
[color=#cf3125]

[font=arial,helvetica,sans-serif][color=#000000] cout << "\n20 seconds remaining";[/font]

[font=arial,helvetica,sans-serif][color=#000000] else if (remaining == 10)[/font]
[color=#cf3125]

[font=arial,helvetica,sans-serif][color=#000000] cout << "\n10 seconds remaining";[/font]

[font=arial,helvetica,sans-serif][color=#000000] cout << remaining << "\n"; //for some reason nothing prints at the[/font]

[font=arial,helvetica,sans-serif][color=#000000] //right time unless I include this line.[/font]

[font=arial,helvetica,sans-serif][color=#000000] Delay(1);[/font]

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

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

[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:[/font]

[font=arial,helvetica,sans-serif][color=#000000]A. bad use of "if" statements[/font]

[font=arial,helvetica,sans-serif][color=#000000]B. somewhere the << is overloaded in a way I don't know about.[/font]

[font=arial,helvetica,sans-serif][color=#000000]I have a really hard time believing it could be B, but it's happened before...[/font]

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

Advertisement
what makes you think that the for loop increments seconds if the Delay isn't included?
The following works fine on my system:

#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);
}


However, may I recommend a switch statement instead:

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);
}
}


... or even more compact:

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);
}
}
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 :)

This topic is closed to new replies.

Advertisement