[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
TCPPStopWatch
Run the program to view output
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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 */Output//clock() and the data type clock_t
- include <time.h> //to include time function
//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(); }
- include <stdio.h>
- include <conio.h> //for getch() and kbhit()
- include <math.h> //for floor()
Run the program to view output
Comments
- This code is also C compatible
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
