Run timer at background

How to run timer at background while compiler waiting input from user , if time is out program terminate.
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <windows.h>
#include <time.h>
#include <ctime>
#include <iomanip>
#include <cstdlib>

using namespace std;



void Press(int key)
{
    switch (key)
    {
        case 0:
        while (true)
        {
            if(GetAsyncKeyState(0x30))
            cout<<"Right!!"<<endl;
            system("pause");
            break;
        }
        break;
        case 1:
        while (true)
        {

            if(GetAsyncKeyState(0x31))
            {
                cout<<"Right!!"<<endl;
                system("pause");
                break;

            }

        }
        break;
        case 2:
        while (true)
        {
            if(GetAsyncKeyState(0x32))
            {
                cout<<"Right!!"<<endl;
                system("pause");
                break;
            }
        }
        break;
        case 3:
        while (true)
        {
            if(GetAsyncKeyState(0x33))
            {
                cout<<"Right!!"<<endl;
                system("pause");
                break;
            }
        }
        break;
         case 4:
        while (true)
        {
            if(GetAsyncKeyState(0x34))
            {
                cout<<"Right!!"<<endl;
                system("pause");
                break;
            }
        }
        break;
         case 5:
        while (true)
        {
            if(GetAsyncKeyState(0x35))
            {
                cout<<"Right!!"<<endl;
                system("pause");
                break;
            }
        }
        break;
        case 6:
        while (true)
        {
            if(GetAsyncKeyState(0x36))
            {
                cout<<"Right!!"<<endl;
                system("pause");
                break;
            }
        }
        break;
         case 7:
        while (true)
        {
            if(GetAsyncKeyState(0x37))
            {
                cout<<"Right!!"<<endl;
                system("pause");
                break;
            }
        }
        break;
         case 8:
        while (true)
        {
            if(GetAsyncKeyState(0x38))
            {
                cout<<"Right!!"<<endl;
                system("pause");
                break;
            }
            break;
        }
         case 9:
        while (true)
        {
            if(GetAsyncKeyState(0x39))
            {
                cout<<"Right!!"<<endl;
                system("pause");
                break;
            }
        }
        break;
    }
}

int main()
{
    cout<<"Welcome !"<<endl;
    system("pause");
    for (int h=0;h<5;h++)
    {
         system("cls");
        cout<<"Level: "<<h+1<<endl;
        cout<<"Time: "<<6-h<<" Seconds"<<endl;
        int xRan[9],yRan;
        srand( time(0));
        int x=2;
        yRan=rand()%9;
        for (int i=0;i<9;i++)
        xRan[i]=rand()%9+1;
        int num=6;

        for (int i=0;i<9;i++)
        {

            cout<<xRan[i]<<setw(5);

        }
        cout<<endl;
        cout<<setw((yRan*5)+1)<<"^"<<endl;
        Press(xRan[yRan]);


    }


    return 0;
}
closed account (G309216C)
Hi,

It is not a easy concept for beginners to attempt the reason is because it requires Multi-Threading and it is not a beginners topic. We can create a new thread using a function using the function CreateThread.
MSDN Documentation:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx

then in that thread create a new thread with a infinite for loop:
for(;;){}

then use WaitForSingleObject function and put it in a if() statement with a comparison to WAIT_TIMEOUT.

MSDN Documentation:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms687032(v=vs.85).aspx

Follow?
I think not it is too complicated for you, trust me don't attempt it yet it will only confuse you and get you tangled up.
Don't worry about it now. Well what I told you is more of a advanced way just to scare you away from this becuase doing this type of programs at this stage is not good.

Thanks!
use multithreading or increment the time by small margin between pressing/not pressing buttons.

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
int key()
{
	switch()
	{
	 //check key pressed
		case 
		case
		case
	}
	return // key pressed or nothing when none
}
main()
{
	while (1)
	{
		
		if (key() == smth) // if key is pressed, do something
		 {
			 switch
			 {
			 do something
			 }
		 }
		 
		check_time // time checking should be so fast, that user wouldnt notice
		if () // if time's up do something else
	}
}
Topic archived. No new replies allowed.