C++ Timer Issue

Associate
Joined
20 Jul 2009
Posts
41
I have made a basic timer in c++ which gets an initial time and stores it in a time_t variable. I then get the current time and use difftime() to see the difference between the two variables in seconds so I can gauge how long the timer has been running for.

What I want to do with this is start the timer after the user has inputting an action and then if nothing has been entered after a certain amount of time, exit. I am using readLine() to get the user input which obviously just waits and doesn't continue with the other code so I cannot check the timer. I thought I would need to use threads so the main can still function as normal and the timer can still count in the background but C++ does not directly support threads?

What would you guys suggest I do to tackle this?

Cheers
 
I'm using Windows 7 and Visual Studio 2008.

What I need to know is how to get user input from the keyboard without actually holding the program. I want to be able to display to the user what keys that have been pressed (and remove them if backspace is pressed). Basically the same output as getline() or getchar(), without the holding of the program. Is this possible?
 
Last edited:
I'm using Windows 7 and Visual Studio 2008.

What I need to know is how to get user input from the keyboard without actually holding the program. I want to be able to display to the user what keys that have been pressed (and remove them if backspace is pressed). Basically the same output as getline() or getchar(), without the holding of the program. Is this possible?

yes its possible i wrote a function that did just that a few months ago.

the key to doing it is using the kbhit() function this returns true or false is a key has been pressed.
 
yes its possible i wrote a function that did just that a few months ago.

the key to doing it is using the kbhit() function this returns true or false is a key has been pressed.

Would you be able to explain the function you wrote please? I'm a bit unsure what to do here, thanks.
 
Would you be able to explain the function you wrote please? I'm a bit unsure what to do here, thanks.

Its quite simples



Code:
void GetString(char *String, int MaxLength)
{
	int StrPos=0;
	char KeyPressed = 0;

	while(KeyPressed != CR)
	{
		if(kbhit())
		{
			
			KeyPressed = getch();
			if ((KeyPressed == CR) && (StrPos == 0))
				KeyPressed=0;
			if ((KeyPressed == DEL) && (StrPos > 0))
			{
				putch(KeyPressed);
				putch(' ');
				StrPos--;
				putch(KeyPressed);
			}
			else if ((((KeyPressed >= 33) && (KeyPressed <= 125)) || KeyPressed == ' ') && (StrPos < (MaxLength-1)))
			{
				String[StrPos++] = KeyPressed;
				putch(KeyPressed);
			}
		}
	}
	String[StrPos]=0;
}
 
Back
Top Bottom