Thread: Timeouts on Input

  1. #1
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23

    Question Timeouts on Input

    Hi,

    I was just wondering is it possible to put a timeout on input in a C++ program (i.e. the user has a set time to enter a value before the program moves on). If this is possible where would I start looking to get such a thing going?

    Cheers guys!

    Dunners
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Well this FAQ may point you in the right direction. Basically in your while loop you would determine if the required amount of time has passed, and if it has or the user pressed enter, you could exit the while loop.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23
    Cheers, I think that might be what I'm looking for, I'll let you know how it goes. Thanks again
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

  4. #4
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23
    That doesn't seem to work. The program just hangs on the getch() until the user inputs a value. I need it to break out as soon as the user enters a value or after a set period of time even if the user hasn't entered a thing. Is this possible?

    Cheers
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Oh, gotcha. Well then I guess you would need to check for when the user pressed a key, you could try a board search for kbhit(). I believe this is also defined in the non standard file conio.h

    I am not sure of a standard way of doing this(that is simply), maybe someone else might?
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Alright, I do not believe there is any easy way to do this, but threads are easy, right?

    Here is the code that does what you want, I will be back later to explain it if necessary(have to go get my daughter dinner):
    Code:
    #include <windows.h>
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    void getInput(LPVOID param);
    
    
    int main() {
    
    	DWORD myThreadID;
    	HANDLE myThread;
    	int userInput = 0;
    
    	cout << "We will start our thread and then sleep for 5 seconds (user timeout)" << endl;
    
    	myThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)getInput, &userInput ,0, &myThreadID);
    
    	WaitForMultipleObjects(1, &myThread, TRUE, 5000);
    	
    	CloseHandle(myThread);
    
    	if(userInput != 0){
    		cout << "User entered " << userInput << endl;
    	}else
    		cout << endl << "Timeout occured" << endl;
    
    	return 0;
    }
    void getInput(LPVOID param){
    	cout << "Enter a number: ";
    	cin >> *((int*)param);
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  7. #7
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23
    Hi,

    I had a fiddle myself and managed to come up with this.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    void main()
    {
      	bool ch;
      	long counter = 0;
      	long ans = 0;
    
      	puts ("Press any key to quit");
      	printf ("%d\n\n", counter);
    
    
    	for(counter = 0; counter != 500000; counter++)
    	{
    
    		ans += 1;
    		ch = kbhit();
    		if(ch == true)
    			break;
    
    	}
    
    	if(ch == true)
    		printf("You Quit The System!\n\n");
    	else
    		printf("You Did Not Quit The System!\n\n");
    
    
    	printf("%d\n", counter);
    	printf("%d\n", ans);
    	system("pause");
    
    }
    That's just the rough code I came up with, it's not easy to set a time for the delay (other than trial and error) but it seems to work. The ans+=1 just wastes some CPU cycles, probably won't leave it in the final version, and I know some people find breaking from loops, etc to be a bit evil but it should be OK.

    Cheers for your help and for that additional code but I think I'll be best using what I came up with as it's for a school project.

    Thanks again!

    Dunners
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Dunners, are you learning C or C++ ? Better decide quick, and start using the appropriate standards. Try not to mix languages unless you have to/need to.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23
    I'm supposed to be using C++ but that chunk of code was a bit of an edit of some older C code I had, I'll tidy it up before I put it in my project. Knowing my teacher if I don't there will be hell to pay!
    "Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
    - Stewie Griffin

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Just for completeness, here's the nice way to do it on Windows.
    Code:
    #include <windows.h>
    
    int main(void)
    {
    	const unsigned int MY_TIMEOUT = 5000; // In milliseconds.
    
    	if (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), MY_TIMEOUT))
    	{
    		// Input waiting, get it!
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  4. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM