[try Beta version]
Not logged in

 
TIMER problem

Oct 22, 2008 at 11:55am
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <conio>
void pause(int seconds);

int main()
{
int a;
time_t rawTime;
struct tm* localTime;
char buffer[1024];

while(true)
{

cout <<"\nThis is a timer program"<<endl;
cout<<"key in your input :"<<endl;
cin >> a;
time(&rawTime);
localTime = localtime(&rawTime);
strftime(buffer, 1024, "\nThe time is: %I:%M%p and %S seconds, and today is %A, %B %d, %Y.", localTime);
puts(buffer);
pause(1);

clrscr();

}
getch();
return 0;
}
void pause(int seconds)
{
clock_t begin = clock() + (seconds * CLOCKS_PER_SEC);
while(clock() <= begin) {}
}

The timer cannot run after i put the cin >> a,is there anyway to separate the timer and the statement?For example i want the program to let me key in the value while the timer keeps running without interuption by keeps changing it's clock value.
Oct 22, 2008 at 12:53pm
The idea is that
time(&rawTime);
reads the time only in that moment when u call the function. So in other words it reads the time from system clock when u use the function. If u want u can use that function again another time or use a loop witch is looking for a specified time.(something like pause() ).


http://www.cplusplus.com/reference/clibrary/ctime/
Last edited on Oct 22, 2008 at 12:58pm
Oct 22, 2008 at 2:03pm
You might also find the Boost library useful in this regard.

http://www.boost.org/doc/libs/1_36_0/libs/timer/timer.htm
Oct 22, 2008 at 2:19pm
I still cannot figure it out ,i tried several other ways but the timer and the cin object cannot occur simultineously.What i want for the program content is about a group of selection menu display first,then the timer appeared after the last part of the menu,cin statement prompt out to let me key in the selection.
Oct 22, 2008 at 4:46pm
One alternative that I know is
kbhit();

This is a function in conio.h that returns a non-zero value in case of key stroke (key hit). So u can do something like:
while(!kbhit()) { //...}
Oct 22, 2008 at 7:17pm
I still cannot figure it out ,i tried several other ways but the timer and the cin object cannot occur simultineously.What i want for the program content is about a group of selection menu display first,then the timer appeared after the last part of the menu,cin statement prompt out to let me key in the selection.


You need to do some study on multi-threaded development. That will let you thread your timer off so it will continuously run while your other code is working. Unfortunately, Multi-threaded development is exponentially harder than normal coding.
Topic archived. No new replies allowed.