Wednesday, 22 January 2014

Timer In C++

#include<iostream.h>
#include<time.h>
#include<conio.h>
void sleep(long double d)
{
clock_t start=clock();
while(clock() - start < d); ///loop until time's up
}
main()
{char alpha;

 do{
 long double h1,m1,n=0;
 cout<<"\t\t\t\t T I M E R \n";
 cout<<"Enter Time: ";
 cout<<"\n Hours   : ";
 long double h,m,s=0;
 cin>>h;
 cout<<"\n Minutes : ";
 cin>>m;
 cout<<"\nSeconds  : ";
 cin>>s;
 if(h<0||h>100)
 cout<<"Unexpected values Entered ... ";
 else if (m<0||m>60)
 cout<<"Unexpected values Entered ... ";
 else if (s<0||s>60)
 cout<<"Unexpected values Entered ... ";
 else
 {h1=h*60*60;
  m1=m*60;
  n=h1+m1+s;
 cout<<"\n Time Elapsed : \n";
 for(int i=0;i<n;i++)
 {
sleep(990);     // 990 and not 1000 to bring accuracy .......................
cout<<".";

 }
 clrscr();
 cout<<"\t\t\t\tT I M E R \n\n\n";
 cout<<"Timer Finished ... for "<<h<<":"<<m<<":"<<s ;
 }
 cout<<"\n\nDo You Wish To Continue ... (y/n) ... ";

 cin>>alpha;
clrscr ();
}while(alpha=='y'||alpha=='Y');

}

No comments:

Post a Comment