I was wondering what method I would use to count upwards in seconds instead of near instantly.
I was wondering what method I would use to count upwards in seconds instead of near instantly.
Well I assume you just want to add a delay to your program correct? Well I would suggest this. Add the header windows.h to your code. Then also add the library winmm.lib to your project. Use the function timeGetTime( void ), which returns a DWORD, the number or milli-seconds since windows has started. Just keep track of them and display a number every 1 second or something might look like this. The following code will print "5" every second. This is just a simple snippet to help you, if you have any questions shoot.
int main( void )
{
UINT uCount = 0;
BOOL bDone = FALSE;
DWORD dwTime1 = 0, dwTime2 = 0;
dwTime1 = timeGetTime();
while( !bDone )
{
if( dwTime2 >= 1000 ) /* For every 1 second */
{
cout << " 5 " << endl;
dwTime1 = timeGetTime();
dwTime2 = 0;
uCount++;
if( uCount == 5 )
bDone = TRUE;
}
else
{
dwTime2 = timeGetTime() - dwTime1;
}
}
return 0;
}
..or you could you use the time.h header file.
it has a difftime() to calc differences [] in two times.
-
the best things in life are simple.
// This will output the elasped time
// I used system("pause") to create
// the interval cin works as well
#include<iostream>
using std::cout;
#include<ctime>
main()
{
time_t start, end;
cout <<"Start\n";
start = clock();
system("pause"); // Wait for a keypress
end = clock();
cout <<"Done\n";
cout << "The interval was : " << (end-start) << " ms.\n";
return (0);
}
Why does the program take a second to react (making the total time more than it should)
I'm assuming that you would need to do some sort of threading if you want real time stopping (stopping the watch at exactly the time you hit the key). Unfortunately I have never messed with threading in C++, only in Java.Originally posted by marcusbankuti
Why does the program take a second to react (making the total time more than it should)