C++ 在执行其他命令时运行计时器c++;

C++ 在执行其他命令时运行计时器c++;,c++,timer,C++,Timer,因此,我正在尝试为一个更大的项目创建一个概念证明。我目前正在做一个计时测验,只有一个问题,你有10秒的时间回答 我真正想问的是什么 我知道我可以通过以下方式读取用户输入 “cin我找到了这个。它很酷,回答了你的问题。作者使用gotoxy()移动到控制台输出的开头并覆盖当前时间。但是我不推荐使用命名空间std;的例程,所以我编辑了代码。祝你好运能将它与你的集成在一起。” #include <iostream> #include <ctime> #include <w

因此,我正在尝试为一个更大的项目创建一个概念证明。我目前正在做一个计时测验,只有一个问题,你有10秒的时间回答

我真正想问的是什么

  • 我知道我可以通过以下方式读取用户输入

    “cin我找到了这个。它很酷,回答了你的问题。作者使用
    gotoxy()
    移动到控制台输出的开头并覆盖当前时间。但是我不推荐使用命名空间std;的
    例程,所以我编辑了代码。祝你好运能将它与你的集成在一起。”

    #include <iostream>
    #include <ctime>
    #include <windows.h>
    
    // to move back to the beginning and output again
    void gotoxy (int x, int y)
    {
        COORD coord; // coordinates
        coord.X = x; coord.Y = y; // X and Y coordinates
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
    }
    
    int main ()  
    {
        std::time_t now;
        while (true)
        {
            gotoxy (0,0);
            now = std::time(0);
            std::cout << "The time is now: " << std::ctime(&now);
            Sleep (20);
       }
        std::cin.get();
        return 0;
    }
    
    #包括
    #包括
    #包括
    //返回到开始并再次输出
    void gotoxy(整数x,整数y)
    {
    COORD COORD;//坐标
    坐标X=X;坐标Y=Y;//X和Y坐标
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),坐标);//移动到坐标
    }
    int main()
    {
    标准时间:现在;
    while(true)
    {
    gotoxy(0,0);
    现在=标准::时间(0);
    std::cout我找到了这个。它很酷,它回答了你的问题。作者使用
    gotoxy()
    移动到控制台输出的开头并覆盖当前时间。但是我不推荐使用命名空间std;
    例程,所以我编辑了代码。祝你好运将它与你的代码集成在一起

    #include <iostream>
    #include <ctime>
    #include <windows.h>
    
    // to move back to the beginning and output again
    void gotoxy (int x, int y)
    {
        COORD coord; // coordinates
        coord.X = x; coord.Y = y; // X and Y coordinates
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
    }
    
    int main ()  
    {
        std::time_t now;
        while (true)
        {
            gotoxy (0,0);
            now = std::time(0);
            std::cout << "The time is now: " << std::ctime(&now);
            Sleep (20);
       }
        std::cin.get();
        return 0;
    }
    
    #包括
    #包括
    #包括
    //返回到开始并再次输出
    void gotoxy(整数x,整数y)
    {
    COORD COORD;//坐标
    坐标X=X;坐标Y=Y;//X和Y坐标
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),坐标);//移动到坐标
    }
    int main()
    {
    标准时间:现在;
    while(true)
    {
    gotoxy(0,0);
    现在=标准::时间(0);
    
    std::cout这是一个使用
    中的实用程序的示例。为此,您必须访问C++11

    您可以轻松地使用此模型使其适应您的代码

    #include <iostream>
    #include <chrono>
    
    using namespace std::chrono;
    
    int main() 
    {   
        int num;
    
        // start timing
        auto start = steady_clock::now();
    
        std::cin >> num;
    
        // stop timing
        auto stop = steady_clock::now();
    
        // stop - start will give you the time that passed
        std::cout << "It took " 
                  << duration_cast<milliseconds>(stop - start).count()
                  << " milliseconds!\n";
    
        std::cin.get();
    }   
    
    #包括
    #包括
    使用名称空间std::chrono;
    int main()
    {   
    int-num;
    //开始计时
    自动启动=稳定的时钟::现在();
    std::cin>>num;
    //停止计时
    自动停止=稳定的时钟::现在();
    //停止-开始会给你过去的时间
    
    std::cout这是一个使用
    中的实用程序的示例。为此,您必须访问C++11

    您可以轻松地使用此模型使其适应您的代码

    #include <iostream>
    #include <chrono>
    
    using namespace std::chrono;
    
    int main() 
    {   
        int num;
    
        // start timing
        auto start = steady_clock::now();
    
        std::cin >> num;
    
        // stop timing
        auto stop = steady_clock::now();
    
        // stop - start will give you the time that passed
        std::cout << "It took " 
                  << duration_cast<milliseconds>(stop - start).count()
                  << " milliseconds!\n";
    
        std::cin.get();
    }   
    
    #包括
    #包括
    使用名称空间std::chrono;
    int main()
    {   
    int-num;
    //开始计时
    自动启动=稳定的时钟::现在();
    std::cin>>num;
    //停止计时
    自动停止=稳定的时钟::现在();
    //停止-开始会给你过去的时间
    
    std::cout当程序等待输入时,时钟继续运行,因此对输入操作计时没有问题(即,查看需要多长时间)但是如果你想在10秒的时间内中止输入操作,那么你就必须使用<强>平台特定的工具,比如键盘轮询。所有的标准C++输入操作都是阻塞的。所以,只有标准C++,你可以尝试将输入操作放在它自己的执行线程中,推理异步。这就是线程的用途。但是你会发现,当线程等待输入时,没有办法终止它,并且(变得有创意)没有办法向它发布虚假输入

    尽管如此,关于

    在计时器请求输入时,您能让它运行吗

    如果你想连续显示时间,那没问题

    你可以把它放在自己的线程中

    但是,如果您希望每行输入超过一个字符,则可能必须使用系统特定的方式,因为使用标准库的文本显示修改功能(基本上由ASCII
    \b
    \r
    控件组成)我能想到的任何方法都会在每次更改显示时间时弄乱文本光标的位置,如下所示:

    #include <atomic>
    #include <chrono>
    #include <exception>    // std::terminate
    #include <iostream>
    #include <iomanip>      // std::setw
    #include <thread>
    using namespace std;
    
    auto fail( string const& s )
        -> bool
    { cerr << "!" << s << "\n"; terminate(); }
    
    auto int_from( istream& stream )
        -> int
    {
        int x;
        stream >> x || fail( "Input operation failed" );
        return x;
    }
    
    namespace g {
        atomic<bool>    input_completed;
    }  // namespace g
    
    void count_presentation( string const& prompt )
    {
        for( int n = 1; not g::input_completed; ++n )
        {
            string const count_display = to_string( n );
            string const s = count_display + prompt.substr( count_display.length() );
            cout << "\r" << s << flush;
    
            using namespace std::chrono_literals;
            this_thread::sleep_for( 100ms );
        }
    }
    
    auto main()
        -> int
    {
        string const prompt = string( 20, ' ' ) + "What's 6*7? ";
        auto counter = thread( count_presentation, ref( prompt ) );
        const int x = int_from( cin );
        g::input_completed = true;
        counter.join();
        cout << x << "\n";
    }
    
    #包括
    #包括
    #include//std::terminate
    #包括
    #include//std::setw
    #包括
    使用名称空间std;
    自动失败(字符串常量(&s)
    ->布尔
    {cerr x | | fail(“输入操作失败”);
    返回x;
    }
    名称空间g{
    完成原子输入;
    }//名称空间g
    无效计数\u表示(字符串常量和提示)
    {
    对于(int n=1;非g::输入_已完成;++n)
    {
    字符串常量计数显示=到字符串(n);
    string const s=count_display+prompt.substr(count_display.length());
    
    cout当程序等待输入时,时钟会继续运行,因此对输入操作计时没有问题(即,查看需要多长时间)但是如果你想在10秒的时间内中止输入操作,那么你就必须使用<强>平台特定的工具,比如键盘轮询。所有的标准C++输入操作都是阻塞的。所以,只有标准C++,你可以尝试将输入操作放在它自己的执行线程中,推理异步。这就是线程的用途。但是你会发现,当线程等待输入时,没有办法终止它,并且(变得有创意)没有办法向它发布虚假输入

    尽管如此,关于

    在计时器请求输入时,您能让它运行吗

    如果你想连续显示时间,那没问题

    你可以把它放在自己的线程中

    但是,如果您希望每行输入超过一个字符,则可能必须使用系统特定的方式,因为使用标准库的文本显示修改功能(基本上由ASCII
    \b
    \r
    控件组成)我能想到的任何方法都会在每次更改显示时间时弄乱文本光标的位置,如示例1所示