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

Recommended Answers

All 15 Replies

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

commented: Preemptive Answer Rep ;) (I know, I saw the other thread) +2
commented: Can you tell me what the rest of this sentence .. +4

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.

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();

>>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....

If its clear all of the text inputted into the console you could use cmd Clear?

Can you please give me the syntax...??
Thanks

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.

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)

commented: Exactly right :) +26

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.