Hello,
I am using turbo c++, dos one, and am making a program, but theres a problem...
My program runs on an infinite loop and has a small delay in between...It takes user's keystrokes at the start of loop like this,

int in;
while(1)
{
   if(kbhit())
     in=getch();
   .
   .
    delay(100);
}

in this case, if the user holds a key, it takes plenty of inputs and keeps on doing work....
Is there a way through which i can clear all the inputs at the end of loop...
Thanks

Edited 6 Years Ago by Nick Evan: Added code-tags

Use a while loop to read all keys, then sleep.

I dont want to do that....
My program works in a bit different way...
The loop keeps on running and the program works, if user presses any key, then the required thing is done.....Is there some other way...?

None that I can be bothered to mention any more, thanks to your vague requirements.

Its not vague...
Can someone else please help....

>>Is there a way through which i can clear all the inputs at the end of loop..

while( kbhit() )
   getche();

Thanks...But this causes another problem....
Actually in the loop an animation takes place and its controlled by the user's input....So if user holds a key....the animation keeps on playing for a long time, but if i will do this....the user will have to tap keys....
Is there something else.....???
Also, is there something like a limit to how many keys are stored in the console...??
I mean if its like only 10 keys can be added in queue.. or something like that....That could do the thing too....

None that I can be bothered to mention any more, thanks to your vague requirements.

Its not vague...
Can someone else please help....

If Salem can't understand what you need, believe us, your description is very vague... You can't prove it isn't by simply saying so.

system()
Cmd Clear

you mean system("cls") on MS-Windows or system("clear"); on *nix computers. That erases everything on the console screen, does nothing for program's variables. And that I NOT probably what Salem was talking about.

Edited 6 Years Ago by Ancient Dragon: n/a

system() is used to run a dos command right ???
But clear isnt a dos command....i tried it on dos directly.....
I think the syntax should be like this,
system("clear");
But it wont work....Sorry, but i didnt got the point...Please can you explain...?
Thanks

or system("clear"); on *nix computers.

system is used to run a command of either dos or *nix on the respective system. He was offering you system("cls") on your Windows baed PC or system("clear") for use in *nix if it was applicable.

However, AD was also saying that this information is not even relevant to your task at hand because this is simply a solution to wipe the console clean of any output and it does nothing to any of your inputs that you are asking how to clear.

Salem was simply saying that we really don't know what you are trying to do so we cannot help without more specifics. If all you want is a keypress to keep something going just throw away the key or never save it in the first place a la getchar() with no lvalue.

(sorry to speak for anyone if they didn't intend it like that)

Edited 6 Years Ago by jonsca: n/a

Comments
Exactly right :)

Well I arrived at this, but apparently it wasn't wanted.

int in;
while(1)
{
   if(kbhit()) {
      in=getch();
      // work on the key
   } else {
      // work to be done if no key was pressed
   }
   // work to be done always
   .
   // make sure no more keys have been pressed
   while ( kbhit() ) getch();
   // now sleep, perchance to dream
   delay(100);
}

perhaps this is what you prefer

void Always_Run( void )
{
	// Always run this crap
}

int main()
{
	while( 1 )
	{
		static char Buffer[ 1024 ];

		Always_Run();

		if ( kbhit() ) // If there is text
		{
			ZeroMemory( &Buffer, sizeof( Buffer ) ); // Clean the buffer

			while( getch() != '\r' ) // Takes Text till return
			{				
				Buffer[ strlen( Buffer ) ] = getch();
				Always_Run();
			}
			// Do crap after text tooken
		}
		else 
		; // No text tooken do other crap
	}
	return NULL;
}

Edited 6 Years Ago by VilePlecenta: n/a

This article has been dead for over six months. Start a new discussion instead.