I'm trying to create a program of a stopwatch using this Standard C-Free 5.0. Here's what I've got so far:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
char button;
int minutes=0, seconds=0, millisec=0;
int main(void)
{
while(1)
{
reset:
button = '\0';
int minutes=0, seconds=0, millisec=0;
printf(" %d : %d : %d ", minutes, seconds, millisec);
system("cls");
if(button == 'a')
{
while(1)
{
cont:
button = '\0';
Sleep(10);
millisec++;
if(millisec == 100)
{
millisec = 0;
seconds++;
if(seconds == 60)
{
seconds = 0;
minutes++;
}
}
printf(" %d : %d : %d ", minutes, seconds, millisec);
system("cls");
if(button == 's')
{
while(1)
{
button = '\0';
printf(" %d : %d : %d ", minutes, seconds, millisec);
system("cls");
if(button == 'a')
{
goto cont;
}
if(button == 'd')
{
goto reset;
}
}
}
}
}
}
}
I'm trying to start the stopwatch with a pressed of button 'a' but it wouldn't work. Using scanf() will pause the whole program. Is there a way to detect a button being pressed and continue the stopwatch program? I mean without pausing the program especially the pressing 's' to stop and pressing 'a' again to continue, while displaying the timer at all times.
This should help _kbhit and it's important to use _getch() after it.
#include <conio.h>
//...
int key;
while (1)
{
if (_kbhit())
{
key = _getch();
if (key == 'a')
printf("You pressed 'a'\n");
else if (key == 'd')
printf("You pressed 'd'\n");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With