Hello, I am new to C/C++ programming, and was inquiring about creating a stopwatch function that starts at 0 sec and counts up to some time when a user tells it to stop. I am stuck and looking for somewhere to start.
Thanks in advance
Printable View
Hello, I am new to C/C++ programming, and was inquiring about creating a stopwatch function that starts at 0 sec and counts up to some time when a user tells it to stop. I am stuck and looking for somewhere to start.
Thanks in advance
This sort of application cannot be done in pure C, you will need to use OS specific functions. What OS are you using?
Down and dirty idea for this.
About five times each second, your C program will get the current time from clock(), (this is just one way to do it, right off the top of my head, sans coffee), which you will subtract from an earlier time, to find and display the number of seconds that have elapsed, so far.
This is just an idea, not a program ready to run:
Your loop will lose a few milliseconds every loop, so you need to set loss to roughly adjust for it. How much you'll lose will depend on your OS and how busy your system is.Code://add any other include files you need
#include <time.h>
//#define ESC - trap the key press
clock_t start, stop, loss;
int keyPressed;
start = clock();
seconds1 = start / TICKS_PER_SEC;
printf("\n\n Press ESC to Quit");
do {
/*pause in milliseconds (the OS will "schedule" the actual delay),
giving it at least 200, but usually more)
*/
delay(200);
stop = clock();
start = stop + loss; //See note
seconds2 = stop / TICKS_PER_SEC;
if(seconds 2 > seconds1) {
seconds1 = seconds2;
total_sec++;
// SetConsoleCursorPosition (or gotoxy() if you have conio.h), is good for this:
printf( Elapsed Time: %d seconds", total_sec);
}
}while(keyPressed != ESC);
Your OS may have high performance counters/timers, which can make your stopwatch more accurate.
Adak: Where is that delay() function coming from?
Oh Gawd! I slipped into Turbo C mode, didn't I? :p :p
Salem was asking "Why would anyone want to use Turbo C?" For me, it's because TC lets me do these kind of little programs with one hand behind my back, blindfolded.
It's apparently accurate for 60 seconds, in an off-hand test. It would need adjusting to be even close to accurate.Code:/* Adak, Sept 23, '09.
stopwatch type program for Turbo C. Adjust loss or delay(),
for increased accuracy.
Status: ok
*/
#include <stdio.h>
#include <time.h> //for clock()
#include <dos.h> //for delay
#include <conio.h> //for getch()
#define ESC 27
//27 = 0x1B
//VK_ESCAPE (0x1B) ESC key- trap the key press maybe for Windows ?
int main(void) {
clock_t start, stop, loss;
int i, seconds1, seconds2, total_sec, keyPress;
loss = 20;
total_sec = 0;
start = clock();
seconds1 = start / CLK_TCK; //divide by TICKS_PER_SEC for Visual C;
for(i = 0; i < 36; ++i)
putchar('\n');
gotoxy(10, 8);
printf("Press ESC to Quit");
do {
/*pause in milliseconds (the OS will "schedule" the actual delay),
giving it at least 200, but usually more)
*/
delay(200);
stop = clock();
start = stop + loss; //See note
seconds2 = stop / CLK_TCK; //TICKS_PER_SEC;
if(seconds2 > seconds1) {
seconds1 = seconds2;
total_sec++;
/* SetConsoleCursorPosition for Windows (Visual C), (or gotoxy() if you have
conio.h), is good for this:
*/
gotoxy(10, 10);
printf( "Elapsed Time: %d seconds", total_sec);
if(kbhit())
keyPress = getch();
}
} while(keyPress != ESC);
printf("\n\n\n\t\t\t press enter when ready");
i = getchar();
return 0;
}
This version gives a more "stopwatch" look to it:
Code:/* Adak, Sept 23, '09.
stopwatch type program for Turbo C. Adjust loss or delay(),
for increased accuracy.
Status: ok
*/
#include <stdio.h>
#include <time.h>
#include <dos.h>
#include <conio.h>
#define ESC 27
//27 = 0x1B
//VK_ESCAPE (0x1B) ESC key- trap the key press
int main(void) {
clock_t start, stop, loss;
int i, seconds1, seconds2, total_sec, keyPress;
loss = 20;
total_sec = 0;
start = clock();
seconds1 = start / CLK_TCK; //TICKS_PER_SEC;
for(i = 0; i < 36; ++i)
putchar('\n');
gotoxy(10, 8);
printf("Press ESC to Quit");
do {
/*pause in milliseconds (the OS will "schedule" the actual delay),
giving it at least what you request, but usually more)
*/
delay(100);
stop = clock();
// start = stop + loss; //See note
// seconds2 = stop / CLK_TCK; //TICKS_PER_SEC;
gotoxy(10,10);
printf("Elapsed Time: %.2f ", (stop-start)/CLK_TCK);
/*
if(seconds2 > seconds1) {
seconds1 = seconds2;
total_sec++;
// SetConsoleCursorPosition (or gotoxy() if you have conio.h), is good for this:
gotoxy(10, 10);
printf( "Elapsed Time: %d seconds", total_sec);
}
*/
if(kbhit())
keyPress = getch();
} while(keyPress != ESC);
printf("\n\n\n\t\t\t press enter when ready");
seconds1 = getchar();
return 0;
}
The error won't accumulate if you get the time from the system every time you print it.
So say if time starts at 0s, and you want to print it out every 10s.
(pseudo-code)
*edit* fixed a typo */edit*Code:start_time = time();
while (true) {
elapsed = time() - start_time;
print(elapsed);
delay(10 - elapsed % 10); /* sleep the remaining time in the 10s (the previous sleep could have overslept) */
}
So it's self-adjusting, and has very little code. I *like* it.
Very slick, Cyberfish! :)