[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
TCPPStopWatch

Stop Watch Program

This program illustrates the use of the time.h header file. Not all features of the time.h header file are mentioned; only features that are relevant to this program are covered.

Important Points

  • clock() - This function returns the processor time elapsed. Returns a value of type clock_t
  • clock_t - Data type returned by clock function. Value of clock_t are in units of clock ticks
  • CLK_TCK - Constant holding the time for one clock tick. (calibrated in seconds)

Code

Lines of focus are highlighted in red.
//stopwa.cpp
//built in Turbo C++ IDE (v3.0 Borland Corp 1992)
//author: deostroll
//created: Oct 12, 2005
//version: 1.0
/*	Program that illustrutates the concept of creating a stop watch		*/
  1. include <time.h> //to include time function
//clock() and the data type clock_t
  1. include <stdio.h>
  2. include <conio.h> //for getch() and kbhit()
  3. include <math.h> //for floor()
//function to return value of key pressed int keypress() { char ch; if(kbhit()) { ch=getch(); return ch; } else//if nothing pressed return 0; } int main() { clrscr(); printf("Press enter to start"); char ch='l';//dummy initialization double min, hour, sec,msec,time; //wait for user to hit enter key while(ch!=13) { ch=getch(); } ch=12;//dummy initialization clock_t start,end;// created two variables of clock_t data type start=clock();//to store initial instance of time for reference _setcursortype(_NOCURSOR);//hiding the cursour while(ch!=13) { end=clock();//getting clock ticks at this point time=(end-start)/CLK_TCK;//total time in seconds //reducing the time sequentially to milliseconds hour=floor(time/3600); time-=hour*3600; min=floor(time/60); time-=min*60; sec=floor(time); time-=sec; msec=floor(time*1000); //displaying the time in respective format gotoxy(10,10); printf("% .0f : % .0f : %2.0f : % .1f",hour,min,sec,msec); ch=keypress();//Check of key was pressed } gotoxy(10,20); printf("Stop Watch Ended"); getch(); }
Output
Run the program to view output

Comments

  • This code is also C compatible


last edited (December 30, 2006) by bilderbikkel, Number of views: 1395, Current Rev: 6 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.