Make a Stopwatch using C++
I know that passionate programmers love to do new things on their computer and show it to others even if its totally useless. So I am sharing this C++ code to my blog readers. Do try it out and let me know how does it work.
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void title();
class tym
{
private:
int hh,mm,ss,ms;
public:
void show()
{
if(ms>9)
{
ms=0;
ss++;
}
else if(ss>59)
{
ss=0;
mm++;
}
else if(mm>59)
{
mm=0;
hh++;
}
cout<<hh<<":"<<mm<<":"<<ss<<":"<<ms;
}
void inc()
{
ms++;
}
void cancel()
{
hh=mm=ss=ms=0;
}
tym()
{
hh=mm=ss=ms=0;
}
};
void title()
{
cout<<"\n press s for start ";
cout<<"\n press p for pause ";
cout<<"\n press t for stop ";
cout<<"\n press r for resume ";
cout<<"\n press c for cancel ";
}
void main()
{
tym t;
char opt;
clrscr();
title();
cout<<"\n\n stopwatch :- ";
opt=getch();
if((opt=='s')||(opt=='S'))
e: {
while(!kbhit())
{
clrscr();
title();
cout<<"\n\n stopwatch :- ";
t.show();
delay(100);
t.inc();
}
opt=getch();
if((opt=='p')||(opt=='P'))
{
while(!kbhit())
{
clrscr();
title();
cout<<"\n\n stopwatch :- ";
t.show();
delay(100);
}
opt=getch();
if((opt=='s')||(opt=='S'))
goto e;
else if((opt=='t')||(opt=='T'))
{
clrscr();
title();
cout<<"\n\n stopwatch :- ";
t.show();
delay(5000);
exit(1);
}
else if((opt=='c')||(opt=='C'))
{
while(!kbhit())
{
clrscr();
title();
cout<<"\n\n stopwatch :- ";
t.cancel();
t.show();
delay(100);
}
opt=getch();
if((opt=='s')||(opt=='S')||(opt=='r')||(opt=='R'))
goto e;
else
exit(1);
}
else if((opt=='r')||(opt=='R'))
goto e;
else
exit(1);
}
else if((opt=='t')||(opt=='T'))
{
clrscr();
title();
cout<<"\n\n stopwatch :- ";
t.show();
delay(5000);
exit(1);
}
else if((opt=='c')||(opt=='C'))
{
while(!kbhit())
{
clrscr();
title();
cout<<"\n\n stopwatch :- ";
t.cancel();
t.show();
delay(100);
}
opt=getch();
if((opt=='s')||(opt=='S')||(opt=='r')||(opt=='R'))
goto e;
else
exit(1);
}
else
exit(1);
}
else
exit(1);
getch();
}
Have you run this program? Let me know your views and suggestion in comments .
And the most important thing is to share this with other geeky programmers like you! Because Sharing is ? ...
0 comments:
Post a Comment
Comment Below. Because I love to hear from YOU