如何在 C 中执行 60 秒的 while 循环?

新手上路,请多包涵

有谁知道我怎样才能让这个循环运行 60 秒然后停止?这是我的代码:

 #include<iostream.h>
#include<conio.h>
int main()
{
    clrscr();
    int a=1;
    int b;
    cout<<"3.";
    b=a*10%7;
    while(b!=0)
    {
        cout<<a/7;
        a=b*10;
        b=a%7;
    }
    getch();
    return 0;
}

原文由 Mr.Matho 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 357
1 个回答

使用 <chrono> 库。

 #include<iostream>
#include<conio.h>
#include<chrono>

int main()
{
    clrscr();
    int a=1;
    int b;
    std::cout<<"3."; //Don't use 'using namespace std;'...
    b=a*10%7;
    std::chrono::time_point start = std::chrono::steady_clock::now();
    while(b!=0)
    {
        std::cout<<a/7;
        a=b*10;
        b=a%7;
        if(std::chrono::steady_clock::now() - start > std::chrono::seconds(60))
            break;
    }
    getch();
    return 0;
}

原文由 Xirema 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题