need timer in math game

im putting my function header in here where the players are adding and i need some help on creating a timer so they only have 10 seconds to answer the question:

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
void add( void ) //function header add
{ //start add
system("cls");
	//local identifiers
	int number1; 
	int number2; 
	int answer = 0;
	int correctanswer;
	int counter;
	int returnvalue;

		//random number generator
		srand((unsigned)time(NULL));
		number1 = rand() % 50;
		srand((unsigned)time(NULL));
		number2 = rand() % 50;

		//equation so the program has a number to compare to the users answer
		correctanswer = number1 + number2;
		
		for (counter = 1; counter <= 5; counter++) //initialization, repetition condition, and increment
		{ //open for
		
		//random number generator
		srand((unsigned)time(NULL));
		number1 = 1 + rand() %50;
		srand((unsigned)time(NULL));
		number2 = 1 + rand() %50;

		//creating a correct answer
		correctanswer = number1 + number2;	
		
		//equation for player one
		printf("\n%s Add: %d + %d: ", player1name, number1, number2, answer);
		returnvalue = scanf("%d", &answer); //users input
		
		//fixing the problem when a letter is entered
		if(returnvalue < 1) { 
		 
		while(returnvalue != '\n') {
		printf("%c", returnvalue);
		returnvalue = getchar();
	} //end while

} //end if
		
		//determining if the answer is right or not
		if ( correctanswer == answer )
		{ //open if
			correct(); //function call to correct
			player1score++; //adding point to player one
		} //close if
		else
		{// open else
			incorrect(); //function call to incorrect
		} //close else
	
		//random number generater
		srand((unsigned)time(NULL));
		number1 = 1 + rand() %50;
		srand((unsigned)time(NULL));
		number2 = 1 + rand() %50;
		
		//creating a correct answer
		correctanswer = number1 + number2;
		
		//equation for player two
		printf("\n\n%s Add: %d + %d: ", player2name, number1, number2, answer);
		returnvalue = scanf("%d", &answer);

		//fixing the problem when a letter is entered
		if(returnvalue < 1) { 
		 
		while(returnvalue != '\n') {
		printf("%c", returnvalue);
		returnvalue = getchar();
	} //end while

} //end if


		//determine if player 2 got it right or wrong
		if ( correctanswer == answer )
		{ //open if
			correct(); //function call to correct
			player2score++; //adding a point to player one's score
		} //close if
		else
		{// open else
			incorrect(); //function call to incorrect
		} //close else

		} //close for

		system("cls"); //clear screen
		
		return; //return to function main

} //end add 



please include headers and anything else i may need
Last edited on
There exists SetTimer winapi function. But you would need handling windows events to catch and process the timer timeout. You need #include <windows.h> in this case.

I don't know how to set a timer in console application. But you can organize a cycle where you should wait and periodically check for user input.
For example, when the timer starts, you remember the system time. I suppose that there is a function that return system time with milliseconds. See "time.h" or "ctime" header. If integer seconds are sufficient precision the function is clock(). Then you make a cycle
1
2
3
4
while(now_time < start_time +10sec && there's_no_user_input) 
{
   do nothing or sleep for 100 ms
} 
could you write that out in coding alittle more?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <time.h>
...
clock_t now = clock(); //current system time in "clocks"
int user_response = 0;
clock_t deadline = now + 10*CLOCKS_PER_SECOND;
while(clock() <= deadline)
{
  if(kbhit()) //if the keyboard intput buffer is not empty
  {
     read_user_input();
     user_response = 1; //the user responded in time
     break;
  }
   Sleep(100); //to not load CPU. May be omitted
}

if(user_response)
   //good
else
  //failed 



kbhit() and sleep() function are platform-dependent (or even platform-existing). There is Sleep(milliseconds) WINAPI function.

Another point, if the user answer consists of more than 1 character then i don't know what must be the reaction of the program if after 10 sec the user have input only a half what he intended.
all this information is great and thank you for your help. so im trying to it into my program and im running into error's that read.'

'CLOCKS_PER_SECOND' : undeclared identifier
'kbhit': identifier not found
'Sleep': identifier not found


do you think you could shine some light on this for me. again im sorry for the ignorance i have on this subject. we haven't even went over time.h in class this is the research part of the project. ill be checking this blog through out the day. this is due by four and if you cant get information to me in time dont sweat it. thanks again for all your help.
I'm sorry, the constant is CLOCKS_PER_SEC

kbhit() is defined in <conio.h>. So that is valid only under windows. Under linux it's possible to make a short assember piece of code to check if there was a keyboard hit.

You may not use Sleep() at all. Or #include <windows.h>.
thats fine and thanks for the help but my insturctor has removed the need for a timer thanks again
Topic archived. No new replies allowed.