在执行其他命令时运行计时器 C++ [英] Running a timer while other commands are being executed c++

查看:28
本文介绍了在执行其他命令时运行计时器 C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试为更大的项目创建概念证明.我目前正在做一个定时测验,只有 1 个问题,你有 10 秒的时间来回答.

So I am trying creating a proof of concept for a bigger project. I am currently working on a timed quiz, that has only 1 question, and you have 10 seconds to answer.

我真正想问的是什么

  • 我知道我可以通过执行

  • I know I can read the users input by doing

"cin <

"cin << Var" or "Var = _getch()"

我可以通过做一个计时器

and I can make a timer by doing

clock_t 定时器;

clock_t timer;

定时器 = 时钟();

timer = clock();

//代码

timer = clock() - t;

timer = clock() - t;

但是你如何把这一切放在一起呢?当它要求输入时,你可以运行一个计时器吗?似乎不会,因为 C++ 逐行执行每个部分并等待它完成后再继续.但必须有办法!这是我想出的……

But how do you put that all together? can you have a timer running while it's asking for input? It doesn't seem like it would, since c++ goes line by line executing each part and waiting until its done before moving on. But there has to be a way! Here is what i have came up with...

bool Question(int Correct) {
    int Answer = 0;
    cin >> Answer;
    if (Answer == Correct) {
        return true;
    }
    else {
        return false;
    }
}

int main() {

cout << "1 + 1 is: ";

clock_t Timer;
Timer = clock();

bool Is_Correct = Question(2);

Timer = clock() - Timer;

cout << "You Answered: ";

if (Is_Correct) {
    cout << "Correct!";
}
else {
    cout << "Wrong!";
}

cout << "\nAnd by the way, you answered the question with " << 10 - (Timer / CLOCKS_PER_SEC) << " Seconds to Spare.\n";

cin.get();
cin.get();

return 0;
}

抱歉,间距有点乱.

推荐答案

当程序等待输入时,时钟会继续运行,因此对输入操作进行计时(即查看花费了多长时间)没有问题.但是,如果您想在 10 秒后中止输入操作,则必须使用特定于平台的方法,例如键盘轮询.所有标准 C++ 输入操作都是阻塞的.因此,仅使用标准 C++,您就可以尝试将输入操作放在它自己的执行线程中,推理异步,这就是线程的用途.但是随后您会发现在等待输入时无法终止该线程,并且(获得创意)无法向其发布虚假输入.

The clock continues running while a program's waiting for input, so there's no problem timing an input operation (i.e. to see how long it took). But if you want to abort the input operation when 10 seconds have lapsed, then you'd have to use platform-specific means, such as keyboard polling. All standard C++ input operations are blocking. So with only standard C++ you could attempt to put the input operation it in its own thread of execution, reasoning that asynchronous, that's what threads are for. But then you'd discover that there's no way to terminate that thread while it's waiting for input, and (getting creative) no way to post faux input to it.

仍然,关于

当它要求输入时,您可以运行一个计时器吗?

can you have a timer running while it's asking for input?

如果您的意思是运行时间显示,为什么,那没问题.

if you mean to have a running display of time, why, that's no problem.

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

You can just put that in its own thread.

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

However, if you want input of more than a single character per line, then you will probably have to use system-specific means, because with the standard library's facilities for text display modification (which essentially consists of the ASCII \b and \r controls) any way to do it that I can think of messes up the text cursor position each time the displayed time is changed, as exemplified below:

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

这篇关于在执行其他命令时运行计时器 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆