Help with Timer

Feb 2, 2012 at 1:42am
ignore the fragmented statements and (currently) useless code, i just want to get this feature working before i continue. Once you run it you will see what i mean. After the "loading" screen, it starts the timer, but i want the i value to reset every time it increases, which would make a much more practical timer. i also want the user to be able to enter data while the timer is counting. (there is no data to be entered now of course but that is why i put the cin at the end.. as a test)

HERE IS THE CODE:

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
#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include"dos.h"

#include "windows.h" 
using namespace std;
int A;
vector <string> possible_answers();
int alphabet[27] = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};	//to assign value to each letter of the alphabet according to scrabble rules


void timer(){
	
for (int i=150;i<=1;i++){
	Sleep(1000);
	cout<<i<<" ";
}
}


void load(){
	cout<<"Loading";
for(int i = 0;i <=2;i++){ //waits 3 seconds, just for cool loading effect....
	cout<<".";	
Sleep(1000);
}
system("cls");//clears screen after loading
}



char char_to_point(char *letter){//need to have defined array above because this methods serves to purposes:
return 0;
}



int main() {

cout<<"Welcome to Text Twist \n you will have 2 and a half minutes to give in in every possible word from"<<endl;
cout<<endl;
load();
cout<<"Here is your first"<<endl;
timer();

cin >> A;
system("pause");//pauses program, prevents from closing
return 0;
}


P.S. i was thinking, in order to do this easier could i jsut make the timer a seperate program then launch it within the code in a seperate console window?
Feb 2, 2012 at 12:12pm
so would i use shell execute or system("") to launch the timer that i create in a new IDE?
Feb 14, 2012 at 1:46pm
#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include"dos.h"
#include "windows.h"
#include <time.h>
#include <stdio.h>
#include <sys/time.h>

struct timeval tv;
time_t seconds;

using namespace std;
int A;
vector <string> possible_answers();
int alphabet[27] = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10}; //to assign value to each letter of the alphabet according to scrabble rules

int SetTimer(struct timeval &tv, time_t sec)
{
time_t seconds;
seconds = time (NULL);
tv.tv_sec=seconds;
tv.tv_sec+=sec;

return 1;
}

int CheckTimer(struct timeval &tv, time_t sec)
{
struct timeval ctv;
time_t seconds;
seconds = time (NULL);
ctv.tv_sec=seconds;
//printf ( "Current seconds: %ld\n", seconds );
if( (ctv.tv_sec > tv.tv_sec) )
{

return 1;
}
else
return 0;
}



}
}


void load(){
cout<<"Loading";
for(int i = 0;i <=2;i++){ //waits 3 seconds, just for cool loading effect....
cout<<".";
Sleep(1000);
}
system("cls");//clears screen after loading
}



char char_to_point(char *letter){//need to have defined array above because this methods serves to purposes:
return 0;
}



int main() {
SetTimer(tv,10); //set up a delay timer

cout<<"Welcome to Text Twist \n you will have 2 and a half minutes to give in in every possible word from"<<endl;
cout<<endl;
load();
cout<<"Here is your first"<<endl;
while(CheckTimer(tv,5) !=1)
{

seconds = time (NULL);


printf(".");


}

cin >> A;
system("pause");//pauses program, prevents from closing
return 0;
}
Feb 14, 2012 at 1:48pm
The function SetTimer can be used to set the waiting time. The timer function uses sys/time.h.
Feb 14, 2012 at 5:06pm
thanks, this kind of works, but here is the problem. I want the user to be able to enter data while the counter is counting, and i want the counter to show how much time has gone by


of course none of the data has been implmented but i want it to look something like this:
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
77
78
79
80
81
82
83
84
85
]
#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include"dos.h"
#include "windows.h"
#include <time.h>
#include <stdio.h>
#include <time.h>

struct timeval tv;
time_t seconds;

using namespace std;
int A;
vector <string> possible_answers();
int alphabet[27] = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10}; //to assign value to each letter of the alphabet according to scrabble rules

int SetTimer(struct timeval &tv, time_t sec)
{
time_t seconds;
seconds = time (NULL);
tv.tv_sec=seconds;
tv.tv_sec+=sec;

return 1;
}

int CheckTimer(struct timeval &tv, time_t sec)
{
struct timeval ctv;
time_t seconds;
seconds = time (NULL);
ctv.tv_sec=seconds;
//printf ( "Current seconds: %ld\n", seconds );
if( (ctv.tv_sec > tv.tv_sec) )
{

return 1;
}
else
return 0;
}







char char_to_point(char *letter){//need to have defined array above because this methods serves to purposes:
return 0;
}



int main() {
	string correctword();
	int i;
	string a;
SetTimer(tv,5); //set up a delay timer

cout<<"Welcome to Text Twist \n you will have 2 and a half minutes to give in in every possible word from"<<endl;
cout<<endl;

cout<<"Here is your first"<<endl;
while(CheckTimer(tv,5) !=1)
{

seconds = time (NULL);

i++;
while(a != correctword){
cin>>a;
}
cout<<"TIME: "<<i;

system("cls");
}

cout<<"done"<<endl;
system("pause");//pauses program, prevents from closing
return 0;
}
Topic archived. No new replies allowed.