473,385 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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++.
May 14 '13 #1
4 2547
weaknessforcats
9,208 Expert Mod 8TB
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.
May 15 '13 #2
divideby0
131 128KB
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 "concurrently."

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.

Expand|Select|Wrap|Line Numbers
  1. time_t t1 = time(0), t2;
  2.  
  3. do { t2 = time(0); } while(t1 + 1 > t2);
  4.  
May 15 '13 #3
In the file std::basic.ios, by adding "char myChar;" under index list and adding "cin >> myChar;" in "_Myt" under "ios_base::copyfmt(_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 :
Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3.     typedef unsigned long int clock;
  4.     typedef unsigned short int antiClock;
  5.     clock myClock = 00;
  6.     antiClock myAntiClock = 100;
  7.     int time = 0;
  8.     int decision;
  9.  
  10.     //Clock timer loop.
  11.     while (true)
  12.     {
  13.         Sleep (1000);
  14.         if (time >= 0)
  15.     {
  16.         myClock += 1;
  17.         cout << "Clock : " << myClock << "." << myClock << endl;
  18.     }
  19.     if (myAntiClock <= 100)
  20.     {
  21.         myAntiClock -= 1;
  22.         cout << "Anti Clock : " << myAntiClock << "." << myAntiClock << endl;
  23.         cin >> decision;
  24.     }
  25.     return 0;
  26. }
  27. }
Aug 2 '14 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 2 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Scott Navarre | last post by:
Hello, I have noticed that when using JavaScript to create a page, the sizes of the text input form elements come out bigger than the size I am trying to set them to. I don't know if it...
8
by: Rafal M | last post by:
Hi, Is it possible to create timer 9600/s in C? Regards, Rafal
2
by: andrewcw | last post by:
I am trying to do a windows service with C#. I am using as a base the VB.NET article in VS, but I thing the WITHEVENTS timer notation is a delegate. Can anyone provide sample code & anh hints. ...
3
by: jam | last post by:
Dear all, I am wrtiing a console application and doing some test on timer, the below is my sample code I got from some site, but I cannot make it work... Error is 'System.Timers.Timer' does not...
2
by: crjunk | last post by:
I'm trying to find a way to create input boxes dynamically on the client side but everything that I've come across works with IE, but not FireFox. On my web page, I have the following input...
5
by: bucchi | last post by:
Hi, I am doing the following in a javascript function: var hiddenElement = document.createElement("input"); hiddenElement .setAttribute("type", "hidden"); hiddenElement...
4
by: tomer | last post by:
Hi, I'm using vb.net. How can I create timer within aspx page that show the time ? Thanks
6
by: marty8917 | last post by:
how do you create a input box that ask for the user name in vb
11
by: cmdolcet69 | last post by:
I have the code below that will evaluate a string of characters. If its less then 12 long it will trigger a message box if its equal it will save the information and close the form. I have this...
8
by: yanaramly84 | last post by:
hello! i need someone to help me.. i need to display two independent timer, this timer need input from keyboard key to stop, terminate and reset the timer.. i am using 8051 microcontroller
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.