The getch() waits for user input so it is not useful for time based checks, also, if you are using TC, which most college students use, then, it don't have threads concepts.
So here is my suggestion,
1. Display Question, time=0
2. answered=0, timeout=0
3. while((answered | timeout) == 0) do loop
3.1. read key from buffer
3.2. check for answer if key pressed, answered = 1, break the loop
3.3. add time from last loop to time, check for timeout, in case of timeout, timeout=1, break the loop
3.4. continue the loop
4 move to next question
Okay, now some C code to read last key pressed,
Code:
#include <dos.h>
union REGS i,o;
struct SREGS s;
/*returns scan code of the key that has been hit*/
//Up : 72 Down : 80 Left : 75 Right : 77
int getkey()
{ while(!kbhit()) delay(25);
i.h.ah=0x00;
int86(0x16,&i,&o);
return(o.h.ah);
// ah : scan code
}
/*returns ascii code of the key that has been hit*/
int xgetkey()
{ while(!kbhit()) delay(25);
i.h.ah=0x00;
int86(0x16,&i,&o);
return(o.h.al);
// al : ascii code
}
unsigned char getShiftStatus() {
i.h.ah=0x02;
int86(0x16,&i,&o);
return(o.h.al);
// Bit No : Status
// 0 : Right shift depressed
// 1 : Left shift depressed
// 2 : Ctrl depressed
// 3 : Alt depressed
// 4 : Scroll lock on
// 5 : Num lock on
// 6 : Caps lock on
// 7 : Insert on
}
For time calcs
Code:
// example from difftime() in TC
first = time(NULL); /* Gets system
time */
delay(2000); /* Waits 2 secs */
second = time(NULL); /* Gets system time
again */
printf("The difference is: %f seconds\n",difftime(second,first));
Hope this helps you.