gpt4 book ai didi

c++ - 一秒计时器

转载 作者:行者123 更新时间:2023-11-30 02:54:46 25 4
gpt4 key购买 nike

<分区>

我需要一个每秒执行 X 次的计时器。我做了这个,但是在程序终止之前它不会打印任何东西,我觉得这很奇怪。如果您将 3 作为计数器,它会在 3 秒后打印所有内容,如果您选择 100,它会在 3 秒后打印所有内容。

如何让它每秒打印一次而不是在终止时立即打印?

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com