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

Mr.*_*tho 1 c++ while-loop

有谁知道如何运行这个循环 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;
}
Run Code Online (Sandbox Code Playgroud)

Xir*_*ema 7

使用<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;
}
Run Code Online (Sandbox Code Playgroud)