Feb 13, 2013 at 9:59am UTC
/* not sure where I am going wrong on this counter which should count to microseconds! Anybody to help?*/
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
using namespace std;
void stopwatch();
int main()
{
system("color E2");
char choice;
do
{
cout<<" STOPWATCH"<<endl;
cout<<"press only the letter"<<endl;
cout<<"[s] START"<<endl;
cout<<"[p] PAUSE"<<endl;
cout<<"[c] CONTINUE(press it twice)"<<endl;
cout<<"[x] EXIT"<<endl;
cout<<"Choice: ";
cin>>("%c", &choice);
if (choice == 's' || choice== 'S')
{
stopwatch();
}
if (choice == 'x' || choice == 'X')
{
exit(1);
}
system("cls");
}
while (choice != 's' || choice !='S' || choice != 'x' || choice != 'X');
getch();
return 0;
}
void stopwatch()
{
int hh,mm,ss,ms,z;
hh=00;
mm=00;
ss=00;
ms=00;
z=1;
while(hh<=24 && z==1)
{ mm=0;
while(mm<=59 && z==1)
{ ss=0;
while(ss<=59 && z==1)
{ ms=0;
while(ms<=10 && z==1)
{
if(kbhit() != 1)
{
ms++;
Sleep(50);
system("cls")
;
printf("\n\n\n\n\n\n\n\n\n\t\t\t\t%02d: %02d: %02d: %02d\n", hh,mm,ss,ms);
printf("\n\t\t\t Press any key(twice) to stop.\n");
int a,b=0;
if(kbhit())
{
a = getch();
if(a == 80 || a == 112) //p
a= getch();
if(a == 67 || a == 99) //c
b = getch();
cout <<"a="<<a<<"b="<<b<< " ";
}
}
else
z=0;
}
ss++;
}
mm++;
}
hh++;
}
printf("\n\n\t\t Press any key to return to the main menu.");
getch();
}
Last edited on Feb 19, 2013 at 4:35pm UTC
Feb 13, 2013 at 11:24am UTC
Not tested, but I think it should work...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
struct Num
{
Num(int c, bool d)
{
num = c;
inc = d;
}
int num;
bool inc;
};
Num A(0, true );
Num B(0, false );
Num C(0, false );
while (1)
{
NewTime = clock();
checkTime += (double )(NewTime - PrevTime);
PrevTime = NewTime;
if (checkTime > (double )(CLOCKS_PER_SEC))//Multiply CLOCK_PER_SEC by how often you want to update
{
if (A.inc)
{
A.num++;
if (A.num == 100)
{
A.num = 0;
B.inc = true ;
}
}
if (B.inc)
{
B.num++;
if (B.num == 100)
{
A.inc = false ;
B.num = 0;
C.inc = true ;
}
}
if (C.inc)
{
C.num++;
if (C.num == 100)
{
B.inc = false ;
C.num = 0;
break ;
}
}
checkTime -= (double )(CLOCKS_PER_SEC);
}
}
Last edited on Feb 13, 2013 at 11:54am UTC
Feb 13, 2013 at 12:49pm UTC
Sorry,
Just copied and pasted...
It its not running...
Feb 13, 2013 at 12:57pm UTC
You could always try writing it yourself.
Just a thought.
Feb 13, 2013 at 1:23pm UTC
Good thought. Been trying to crack all day....
Feb 13, 2013 at 1:46pm UTC
@coder777
Ha ha. Incidentally am not doing for the teacher... I am doing it for myself... Besides I do not fear saying what I don't know.
OK looks good but honestly a good counter will change from one number to another without forming an array. Like continuously count from 1 to 100... Just display one number at a time. That way I will know I am in number one number 10 or whichever number... Just like what a digital stop watch will do... well sort of...
That is my biggest challenge.... especially the bit of counting / dispalying 1 to 100 numbers one number at a time!!!
Last edited on Feb 13, 2013 at 4:22pm UTC
Feb 13, 2013 at 7:31pm UTC
Try this, does it do what you need? I sort of brute-forced it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include <iostream>
#include <ctime>
#define MAX_T 3
using std::cout;
struct Num
{
Num(int c, bool d)
{
num = c;
inc = d;
}
unsigned num;
bool inc;
};
int main()
{
const unsigned mtime = MAX_T;
Num A(0, false );
Num B(0, false );
Num C(0, true );
double checkTime = 0;
clock_t NewTime = clock();
clock_t PrevTime = NewTime;
while (1)
{
NewTime = clock();
checkTime += (double )(NewTime - PrevTime);
PrevTime = NewTime;
if (checkTime >= (double )(CLOCKS_PER_SEC))//Multiply CLOCK_PER_SEC by how often you want to update
{
if (C.inc)
{
C.num++;
if (C.num == mtime)
B.inc = true ;
}
if (B.inc)
{
B.num++;
B.inc = false ;
C.num = 0;
if (B.num == mtime)
A.inc = true ;
}
if (A.inc)
{
A.num++;
A.inc = false ;
B.num = 0;
if (A.num == mtime)
{
A.num = 0;
//cout << A.num << " : " << B.num << " : " << C.num << "\n";
//break; <--uncomment both lines to stop here or just let it keep running
}
}
cout << A.num << " : " << B.num << " : " << C.num << "\n" ;//This will print each time on a new line
//cout << A.num << " : " << B.num << " : " << C.num << "\r" << std::flush; <--this will print time over the original
//cout << "\b\b\b\b\b\b\b\b\b" << A.num << " : " << B.num << " : " << C.num << std::flush; //<--this will print time over the original
checkTime -= (double )(CLOCKS_PER_SEC);
}
}
return 0;
}
This one works just like a real timer in base
3 .
Output (Set to 3 so it is not to long):
0 : 0 : 1
0 : 0 : 2
0 : 1 : 0
0 : 1 : 1
0 : 1 : 2
0 : 2 : 0
0 : 2 : 1
0 : 2 : 2
1 : 0 : 0
1 : 0 : 1
1 : 0 : 2
1 : 1 : 0
1 : 1 : 1
1 : 1 : 2
1 : 2 : 0
1 : 2 : 1
1 : 2 : 2
2 : 0 : 0
2 : 0 : 1
2 : 0 : 2
2 : 1 : 0
2 : 1 : 1
2 : 1 : 2
2 : 2 : 0
2 : 2 : 1
2 : 2 : 2
0 : 0 : 0
0 : 0 : 1
0 : 0 : 2
0 : 1 : 0
Last edited on Feb 13, 2013 at 11:50pm UTC