Thread: Time controlled user-input

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    algorism: many thanks for your reply. i need to give it my undivided attention which I should be able to do over the upcoming w/e and will get back to you then. regards, sean

  2. #2
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    algorism: not sure what would be the Windows' equivalents of:
    Code:
    #include <sys/select.h>
    #include <termios.h>
    these would be required to define a windows version of reset_terminal_mode() in your program
    my searches have taken me to Console WinEvents (Windows) but I'm not sure if I'm looking at the right places or, at the very least, in the right directions – apart from this outstanding (and it's a significant outstand no doubt), I've tried to incorporate some of your other suggestions and my current version is looking like this:
    Code:
    #include <iostream>
    #include <string>
    #include <future>
    #include <chrono>
    #include <conio.h>
    
    
    int main()
    {
        using namespace std::chrono_literals;
        std::string name{};
        std::atomic<bool> ready{false};
    
    
        auto tp = std::chrono::system_clock::now() + std::chrono::seconds(5);
    
    
        std::future<void> f =
        std::async(std::launch::async, [&name, &ready]()
        {
            std::cout << "enter your name \n";
            getline(std::cin, name);
            if(kbhit())
            {
                std::this_thread::sleep_for(5ms);
                //small chance of false positive
                ready = true;
            }
            std::cout << "well in time, your name is: " << name << "\n";
        });
    
    
        std::future_status s = f.wait_until(tp);
        if(!ready)
        {
            //reset_terminal_mode();
        }
        if (s == std::future_status::ready)
        {
            f.get();
        }
        else
        {
            std::cout << "took too long, enter first name now \n";
            std::string first_name{};
            getline(std::cin, first_name);
            std::cout << "now enter last name \n";
            std::string last_name{};
            std::cout << "full name is: " << first_name << " " << last_name << "\n";
        }
    }
    If anybody has any Windows-based suggestions on how to wrest control back from console if no input within certain time I'd be very grateful. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "real-time" user-input and graphics
    By laczfinador in forum C Programming
    Replies: 4
    Last Post: 03-15-2011, 10:52 AM
  2. time limit for user input
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 03:01 PM
  3. Idle time of the user
    By cornholio in forum Linux Programming
    Replies: 6
    Last Post: 12-02-2005, 12:51 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM

Tags for this Thread