Question

I want to make a stop-watch in C (live Stop-watch) without using inbuilt function "Timer" in Turbo C. My code is as follows:

 #include<stdio.h>
 #include<conio.h>
 #include<dos.h>

 int main()
 {
 int hh,mm,ss;
 hh=mm=ss=0;
 gotoxy(10,10);
 printf("\nSTOP - WATCH: ");
 gotoxy(20,18);
 printf("HH : MM : SS");
 _setcursortype(_NOCURSOR);
 for(;;ss++) //An infinite loop
 {
  if(ss==60)
  {
    mm++;
    ss=0;
  }

  if(mm==60)
  {
    hh++;
    mm=0;
  }

  gotoxy(20,20);
  delay(1000);
  printf("%02d : %02d : %02d",hh,mm,ss);

 }  
 return 0;
 }

Now I want to exit from this program on press of a button on the keyboard (lets say 'Q').

Was it helpful?

Solution

Use kbhit() and getch() from <conio.h> to get keyboard input.

OTHER TIPS

Your program is to use kbhit() and getch(). Should you forget, should you mistakenly compile, then your only option is Ctrl + Break (which probably won't work, but you might get lucky).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top