Run for a certain amount of time.

Hello, I am trying to write a program that checks if a person presses an arrow key within a 2 second period. If the arrow is not in a two second period, the program closes. I understand how to get the arrow key with kbhit and getch, however, I do not know how to test it for only those two seconds. Any help would be appreciated.
Thank You,
Stronghead
Use clock().
Its defined in the header ctime
Alright I'll look into it, thanks a lot.
Hello, I have been experimenting for a few hours, and I still can not understand how to get the clock function to work. Can someone please help me.
The standard clock() function is not the right answer.

You need a system-dependent function which waits on a key press with a timeout. The following will work on Windows or any POSIX system (like Linux or Mac OS X):

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
// iskeypressed()           - is a key press waiting to be read right now?
// iskeypressed( 1000 )     - wait up to a second for a key press before timeout
//                            (specify max wait timeout in milliseconds)
// iskeypressed( INFINITE ) - wait for a key press with no timeout (i.e. forever)

#ifdef _WIN32

#include <windows.h>

bool iskeypressed( unsigned timeout_ms = 0 )
  {
  return WaitForSingleObject(
    GetStdHandle( STD_INPUT_HANDLE ),
    timeout_ms
    ) == WAIT_OBJECT_0;
  }

#else

#include <unistd.h>
#include <poll.h>

#define INFINITE (-1)

bool iskeypressed( unsigned timeout_ms = 0 )
  {
  struct pollfd pls[ 1 ];
  pls[ 0 ].fd     = STDIN_FILENO;
  pls[ 0 ].events = POLLIN | POLLPRI;
  return poll( pls, 1, timeout_ms ) > 0;
  }

#endif 

Hope this helps.
Last edited on
Thank you very much for your help , however, I'm farily new to programming, and I didn't understand any of that. If you don't mind, would it be possible for you to help me with my code. The project is basically a copy of dance dance revolution for the keyboard. The current bugs are the fact that the game will not continue until you press a key, and that only about 30% of the hits count.
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <conio.h>
#include <math.h>
#include <ctime>
using namespace std;

void _checkarrow(int randomarrow,int &score)
{
	char checkkey;
	if (randomarrow == 1)
		{
			checkkey = _getch();
			cout << checkkey;//Debugging purposes
			if (checkkey == -32 || checkkey == 0)
				{	
					checkkey = _getch();
					switch (checkkey)
					{
						case 72:
							score++;
							cout << "Clicked";//Debugging purposes
							break;
						default:
							break;
					}

				}
		}
		if (randomarrow == 2)
		{
			_kbhit();
			checkkey = _getch();
			cout << checkkey;
			if (checkkey == -32 || checkkey == 0)
				{	
					checkkey = _getch();
					switch (checkkey)
					{
						case 80:
							score++;
							cout << "Clicked";
							break;
						default:
							break;
					}

				}
		}
		if (randomarrow == 3)
		{
			_kbhit();
			checkkey = _getch();
			cout << checkkey;
			if (checkkey == -32 || checkkey == 0)
				{	
					checkkey = _getch();
					switch (checkkey)
					{
						case 75:
							score++;
							cout << "Clicked";
							break;
						default :
							break;
						
					}

				}
		}
		if (randomarrow == 4)
		{
			_kbhit();
			checkkey = _getch();
			cout << checkkey;
			if (checkkey == -32 || checkkey == 0)
				{	
					checkkey = _getch();
					switch (checkkey)
					{
					case 77:
						score++;
							cout <<"Clicked";
					default:
						break;
						
					}

				}

}
}


void main()
{
	
	int randomarrow = 0;
	int score = 0;
	char checkkey;
	srand(time(0));
	for (int counter = 0; counter < 10; counter++)
	{
		
		
		randomarrow = 1 + rand() % 4;
		_sleep(1000);
		if (randomarrow == 1)
		{
			cout << "                 *              \n";
            cout << "               *   *            \n";
            cout << "             *       *          \n";
            cout << "           *           *        \n";
            cout << "         *       *       *      \n";
            cout << "                 *              \n";
            cout << "                 *              \n";
            cout << "                 *              \n";
            cout << "                 *              \n";
            cout << "                 *              \n";
            cout << "                 *              \n";
            cout << "                 *              \n";
            cout << "                 *              \n";
		}
		if (randomarrow == 2)
		{
			cout<< "                     *                           \n";
			cout<< "                   *                             \n";
			cout<< "                 *                               \n";
			cout<< "               *                                 \n";
			cout<< "             *        ************************   \n";
			cout<< "               *                                 \n";
			cout<< "                 *                               \n";
			cout<< "                   *                             \n";
			cout<< "                     *                           \n";
		}
		if (randomarrow == 3)
		{
			cout << "                  *             \n";
			cout << "                  *             \n";
			cout << "                  *             \n";
			cout << "                  *             \n";
			cout << "                  *             \n";
			cout << "                  *             \n";
			cout << "                  *             \n";
			cout << "                  *             \n";
			cout << "                  *             \n";
			cout << "          *               *     \n";
			cout << "            *           *       \n";
			cout << "              *       *         \n";
			cout << "                *   *           \n";
			cout << "                  *             \n";
		}
		if (randomarrow == 4)
		{
			cout<< "                                *         \n";
			cout<< "                                  *       \n";
			cout<< "                                    *     \n";
			cout<< "                                      *   \n";
			cout<< "        ************************        * \n";
			cout<< "                                      *   \n";         
			cout<< "                                    *     \n";
			cout<< "                                  *       \n";
			cout<< "                                *         \n";
		}
		else
			;
	
		_sleep(1000);
		_checkarrow(randomarrow,score);
		_sleep(500);
		system("CLS");
		}

		
		
	

	cout <<"Your score is: " << score <<"\n";
	
}


Thank You Very Much
Topic archived. No new replies allowed.