Jitzu Zu
Jitzu Zu

Reputation: 71

Stopwatch program in standard C

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.

Upvotes: 3

Views: 24024

Answers (4)

KevinIT
KevinIT

Reputation: 7

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<time.h>
#include<windows.h>
main()
{
int choice, h,m,s; h=0; m=0; s=0; //--variable declaration--//
char p= 'p';
printf("Press 1 to start the timer\nPress 2 to exit\n");
printf("\nEnter your choice\n");
scanf("%d",&choice);
switch(choice) //--switch case --//
{
case 1:
{
while(1) //--while condition is true//
{
if(s>59) //--if seconds(s) is > 59--//
{
m=m+1; //--increment minute by 1--//
s=0;
}
if(m>59) //--if minutes(s) is > 59--//
{
h=h+1; //--increment hour by 1--//
m=0;
}
if(h>11) //--if hour(h) is > 11--//
{
h=0; //-Hour to 0--//
m=0;
s=0;
}
Sleep(1000); //--inbuilt function for 1sec delay--//
s=s+1;
system("cls"); //--Clear screen--//
printf("DIGITAL CLOCK");
printf("\n\nHOUR:MINUTE:SECOND");
printf("\n\n%d:%d:%d",h,m,s); //--Print time--//
printf("\n\nTo pause : press P\n");
if(kbhit()) //--Check if any button is pressed on keyboard--//
{
if(p==getch()) //--Check if P is pressed--//
{
system("pause"); //--Inbuilt function for pause and resume--//
}
}
}
break;
}
case 2:
exit(0); //--Exit --//
default:
{
printf("Wrong Choice");
}
}
getch(); //--Holding the screen--//
return 0;
}

Upvotes: 0

masoud
masoud

Reputation: 56479

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");
    }
}

Upvotes: 3

hyde
hyde

Reputation: 62797

Since you use system("cls");, this is probably on dos / Windows command prompt. You can try to see if conio.h is supported by your compiler.

If it is, kbhit() or _kbhit() (link to MSDN, you should check docs of your compiler's libraries for most accurate reference) seems to be what you need to use.

Upvotes: 2

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36401

This is a system problem not C. In general, your hosting system provide buffering to inputs, so when you press a key, it is not delivered at that time to your program, it is buffered until some condition occurs (basically, an end-of-line is pressed).

Under Windows there is different calls you should make to get a keypress.

Under Unix, you should put your tty in non-canonical mode (there is a set of magic calls to tcgetattr and tcsetattr).

See that one for example

Upvotes: 0

Related Questions