Search code examples
c++c++-chrono

How do I convert std::chrono to int


I have been trying to make a wpm counter and, long story short, am running into an issue: After using the high resolution clock, I need to divide it by 12, but I can't convert std::chrono to int. Here is my code:

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

Are there any alternative methods I could also use to using std::chrono for this?


Solution

  • std::chrono will elegantly do this problem with a few minor changes...

    • Instead of Sleep(1000);, prefer this_thread::sleep_for(1s);. You'll need the header <thread> for this instead of <Windows.h> and <synchapi.h>.

    • cin >> text; will only read one word. To read the entire line you want getline(cin, text);.

    • It is easiest to first compute minutes per word stored in a floating point type. To get the total floating point minutes you can just divide the duration by 1.0min. This results in a long double. Then divide that total minutes by 12 to get the minutes per word:

    .

    auto mpw = (stop - start) / 1.0min / 12;
    
    • Then just invert mpw to get words per minute:

    .

    float wpm = 1/mpw;
    

    In summary:

    #include <chrono>
    #include <iostream>
    #include <thread>
    
    using namespace std;
    using namespace std::chrono;
    
    int main()
    {
        typedef steady_clock Time;
        typedef 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;
        this_thread::sleep_for(1s);
        cout << "4" << endl;
        this_thread::sleep_for(1s);
        cout << "3" << endl;
        this_thread::sleep_for(1s);
        cout << "2" << endl;
        this_thread::sleep_for(1s);
        cout << "1" << endl;
        this_thread::sleep_for(1s);
        cout << "GO" << endl;
    
        auto start = Time::now();
        getline(cin, text);
        auto stop = Time::now();
    
        auto mpw = (stop - start) / 1.0min / 12;
        float wpm = 1/mpw;
    
        if (ctext == text)
        {
            cout << "Correct! WPM: " << wpm << '\n';
        }
        else
        {
            cout << "You had some errors. Your WPM: " << wpm << '\n';
        }
    }