How to create a timer that allows input?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Johnny123
    New Member
    • May 2013
    • 1

    How to create a timer that allows input?

    I am trying to create a five second timer that allows a value to be entered in that span of time. However, if a value is not entered within the five seconds, the timer ends and a value will no longer be able to be entered. If it helps, I am trying to create a math test that has a five second time limit for equations to be answered. I am new to programming and any help would be appreciated. Thanks. EDIT -- I forgot to mention that this is in C++.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This is not a beginner's problem.

    What happens is you have to wait on the user to enter a value. While you wait, your programs not running. If your program's not running, there's no way to time 5 seconds.

    Ideally, you would write your own cin from scratch. Not for beginner's. Or your could create an additional thread and give that thread 5 seconds to get a value or you delete the thread from the main program. This get's you into multithreading which is also not a beginner's problem.

    Comment

    • divideby0
      New Member
      • May 2012
      • 131

      #3
      Along with what wfc has stated, it's not something that can be done with standard c/c++ functions.

      If your compiler supports it, you may be able to use the non-standard function _kbhit() to avoid the buffered input functions like cin, getline, etc from blocking your timer and allowing it run "concurrent ly."

      To see if your compiler supports the function, try adding the conio.h header to your project. If you're able to overcome this obstacle, then a very crude "seconds" timer can be made using time_t objects.

      Code:
      time_t t1 = time(0), t2;
      
      do { t2 = time(0); } while(t1 + 1 > t2);

      Comment

      • Sanketb369
        New Member
        • Aug 2014
        • 1

        #4
        In the file std::basic.ios, by adding "char myChar;" under index list and adding "cin >> myChar;" in "_Myt" under "ios_base::copy fmt(_Right);", It will ask you for consol output by holding time. depends on how much characters you put that much time will move forward.

        In the timer.cpp file add these :
        Code:
        int main ()
        {
        	typedef unsigned long int clock;
        	typedef unsigned short int antiClock;
        	clock myClock = 00;
        	antiClock myAntiClock = 100;
        	int time = 0;
        	int decision;
        
        	//Clock timer loop.
        	while (true)
        	{
        		Sleep (1000);
        		if (time >= 0)
        	{
        		myClock += 1;
        		cout << "Clock : " << myClock << "." << myClock << endl;
        	}
        	if (myAntiClock <= 100)
        	{
        		myAntiClock -= 1;
        		cout << "Anti Clock : " << myAntiClock << "." << myAntiClock << endl;
        		cin >> decision;
        	}
        	return 0;
        }
        }

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          The variable decision is not used anywhere except the cin>> plus you have a return 0 inside the while loop so the loop never cycles at all.

          Comment

          Working...