我需要一个每秒执行 X 次的计时器。我做了这个,但是在程序终止之前它不会打印任何东西,我觉得这很奇怪。如果您将三个作为计数器,它会在三秒后打印所有内容,如果您选择它,则打印 100。
如何让它每秒打印一次,而不是在终止时一次打印?
int main()
{
using namespace std;
//Number to count down from
int counter = 10;
//When changed, a second has passed
int second = (unsigned)time(NULL);
//If not equal to each other, counter is printed
int second_timer = second;
while (counter > 0) {
second = (unsigned)time(NULL);
while (second != second_timer) {
//Do something
cout << counter-- << ", ";
//New value is assigned to match the current second
second_timer = second;
}
}
cout << "0" << endl;
return 0;
}