Search code examples
c++countdown

Check if a user has finished typing before the end of a countdown


I'm programming an Uno game in C++.

If a player has only two cards in his game, he has to type "Uno" after playing one card. And I want to check if he has finished typing before the end of a countdown (3 sec for example).

How can I do that ? I've looked up the Clock library but without success.

Thanks :)


Solution

  • Here's a simple example of how to get the time spent waiting for user input:

    auto start = std::chrono::steady_clock::now();
    std::string input;
    std::getline(std::cin, input); // Wait for user input.
    auto end = std::chrono::steady_clock::now();
    auto timeTaken = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();
    std::cout << "Waiting for input took: " << timeTaken << " s" << std::endl;