Basically If the user does not enter a string or character or press any keys after a certain length of time, program will goto or ask again.. or do whatever..

Problem: Not allowing the user to enter input.. Note accepting user input..

#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;

string pword;

int main()
{
printf("You have 5 seconds to Enter the Password: ");
static HANDLE stdinHandle;
// Get the IO handles
// getc(stdin);  //If I take out the // from infront of getc(stdin); user can type but it doesnt accept it..
stdinHandle = GetStdHandle(STD_INPUT_HANDLE);

while (1)
{
DWORD rc = WaitForSingleObject(stdinHandle, 5000);
if( rc == WAIT_TIMEOUT )
{
//printf("Timeout...");
return 0;
}
else if( rc == WAIT_OBJECT_0 )
{
INPUT_RECORD r[512];
DWORD read;
ReadConsoleInput(stdinHandle, r, 512, &read);
}
else if( rc == WAIT_FAILED )
{
 printf("Error:%d.", GetLastError());
}
else
{
 cin>>pword;
 if(pword == "correct")
 {
          cout<<"Nicely done!";
 }
}
}

return 0;
}

Are you looking for help... or is this just a post showing some work that you've done?

Try creating a new thread for accepting input and manage the timeout in your main:

#include <windows.h>

namespace jsw {
    namespace threading {
        class auto_event {
        public:
            auto_event(): _event(CreateEvent(0, false, false, 0)) {}

            BOOL wait(DWORD timeout = 1) const
            {
                return WaitForSingleObject(_event, timeout) == WAIT_OBJECT_0;
            }

            BOOL set() { return SetEvent(_event); }
        private:
            HANDLE _event;
        };

        class thread {
        public:
            static thread start(
                LPTHREAD_START_ROUTINE fn, LPVOID args = 0, 
                DWORD state = 0, DWORD timeout = 5000)
            {
                return thread(CreateThread(0, 0, fn, args, state, 0), timeout);
            }

            static void sleep(DWORD milliseconds) { Sleep(milliseconds); }
            static void exit(DWORD exitCode) { ExitThread(exitCode); }
        public:
            thread(HANDLE thread, DWORD timeout): _thread(thread), _timeout(timeout) {}
            ~thread() { CloseHandle(_thread); }

            DWORD exit_code() const
            {
                DWORD exitCode = NO_ERROR;

                GetExitCodeThread(_thread, &exitCode);

                return exitCode;
            }

            HANDLE handle() const { return _thread; }
            BOOL is_alive() const { return exit_code() == STILL_ACTIVE; }
            DWORD join() { return WaitForSingleObject(_thread, _timeout); }
            DWORD suspend() { return SuspendThread(_thread); }
            DWORD resume() { return ResumeThread(_thread); }
            BOOL abort(DWORD exitCode) { return TerminateThread(_thread, exitCode); }
        private:
            HANDLE _thread;
            DWORD _timeout;
        };
    }
}

#include <iostream>
#include <string>

DWORD WINAPI get_password(LPVOID args)
{
    using namespace jsw::threading;

    std::string *s = (std::string*)((LPVOID*)args)[0];
    auto_event *e = (auto_event*)((LPVOID*)args)[1];

    getline(std::cin, *s);
    e->set();

    return NO_ERROR;
}

int main()
{
    using namespace jsw::threading;

    std::string password;
    auto_event e;
    LPVOID args[2] = {&password, &e};

    thread worker = thread::start(get_password, args);

    if (e.wait(5000))
        std::cout<<'\''<< password <<"' was correct\n";
    else {
        worker.abort(NO_ERROR);
        std::cout<<"Invalid password\n";
    }
}

Wow narue did u write all that just for this post?? I hope u already had that or its just an example cuz thats a lot of code!! Im going to study it and understand it.. I do not fully understand threads but with that, I will now try my best to understand what has been done. I will say the code works perfectly as is.. But I dont just want it to work, I wanna learn why it works and how. So thank you very much...

Hmm, I see that this thread has just been closed.

I wanted to ask if Narue could add a few comments about her offering to explain what's happening here - I would also like to understand more is this.

This question has already been answered. Start a new discussion instead.