【发布时间】:2021-12-20 11:49:43
【问题描述】:
我一直在尝试制作一个 wpm 计数器,长话短说,我遇到了一个问题:
使用高分辨率时钟后,需要将其除以 12,但无法将std::chrono 转换为int。这是我的代码:
#include <iostream>
#include <chrono>
#include <Windows.h>
#include <synchapi.h>
using namespace std;
using namespace std::chrono;
int main()
{
typedef std::chrono::high_resolution_clock Time;
typedef std::chrono::seconds s;
string text;
string ctext = "One two three four five six seven eight nine ten eleven twelve";
cout << "Type: "<< ctext << " in" << "\n" << "5" << endl;
Sleep(1000);
cout << "4" << endl;
Sleep(1000);
cout << "3" << endl;
Sleep(1000);
cout << "2" << endl;
Sleep(1000);
cout << "1" << endl;
Sleep(1000);
cout << "GO" << endl;
auto start = Time::now();
cin >> text;
auto stop = Time::now();
float wpm = stop - start / 12;
if (ctext == text)
{
cout << "Correct! WPM: " << wpm;
}
else
{
cout << "You had some errors. Your WPM: " << wpm;
}
}
是否有任何替代方法我也可以用来为此使用 std::chrono ?
【问题讨论】:
-
你正在使用
std::chrono(并且没有std::int) -
start和stop是两个时间点,stop - start的结果是std::chrono::duration,所以,(stop - start).count()应该给你一个整数。 -
@Yves 你的评论应该是一个答案;)
-
当你使用
std::chrono时,我建议使用std::this_thread::sleep_for(1s)而不是Sleep(1000)。