Yes, I have read the sticky MANY times, however it does not help me with my problem.

The issue is that I have a program where getch() is used in conjunction with kbhit() in order to only recieve input when the user hits a key. The problem is that if the program does something else, while it is not checking for user input, the user can jam keys enough to build up a large amount of key hits, all of which get inputted at once when the program begins checking for input again.

So what I need to do is clear the input stream, however, all the methods I have seen so far require the user to hit Enter in order to get past the input clearing, I need a way to clear input, without requiring an Enter hit.

Thanks in advance.

kalia247 commented: i have same prob... couldn't find on the Net anywhere... but here another person has same prob which no one seems to have a solution for....... great goin'!! +0

Recommended Answers

All 11 Replies

I don't believe the ignore() method for input streams requires the enter key to be pushed before execution occurs.

I did cin.ignore(80, '\n'); however it required me to hit enter, is there another ignore() that you're thinking of?

Can I draw your attention to this snipped post by Narue in the input stream clearing thread

#include <istream>

void ignore_line ( std::istream& in )
{
  char ch;

  while ( in.get ( ch ) && ch != '\n' )
    ;
}

Chris

Freeky -- again, that code requires the use to hit the Enter key

Nishinoran: since your program is already using kbhit() when the program returns to the place to begin getting keys from the keyboard you can flush all the keys with another loop using kbhit

while( kbhit() )
   getche(); // get the key and toss it into the wind
commented: FRIGGIN EPIC! +1

I though that it also breaks out of the loop when there are no more characters left in the buffer, as well as breaking when it reaches a newline delim. Thus it would remove all characters and then break out of the loop leaving the input stream empty with or without a line break?

Chris

I also don't understand. According to both the cplusplus and C-C++ Reference websites (both of which many people find to be reputable online reference sources, though not definitive references, I'll grant you) ignore() takes 2 parameters: the number of char to ingore (which defaults to 1) and the delimiting char (which defaults to EOF). The point being, any legal char can act as the delimiting char, it doesn't have to be the newline char. Therefore, if the delimiting char doesn't have to be the new line char, why shuold you have to push the enter key to get the function to run, or to stop running? As an example, I believe the following snippet is one commonly advised outline for data validation.

int target;
cout << "enter a value for target" << endl;
while(cin >> target)
{
   if(cin.fail())
   {
      cin.clear();
      cin.ignore(1080);
      cout << "input error. reenter a value for target" << endl;
   }
    else
      break;
}

Any further clarification gratefully accepted.

Can I draw your attention to this snipped post by Narue in the input stream clearing thread

#include <istream>

void ignore_line ( std::istream& in )
{
  char ch;

  while ( in.get ( ch ) && ch != '\n' )
    ;
}

Chris

I've attempted to put that line into my program, however I always get some sort of error when I do it:
[img]http://img264.imageshack.us/my.php?image=errorag9.png[/img]

Is there something I'm doing wrong, or is it just my compiler? MS VS 6.0

Freeky -- again, that code requires the use to hit the Enter key

Nishinoran: since your program is already using kbhit() when the program returns to the place to begin getting keys from the keyboard you can flush all the keys with another loop using kbhit

while( kbhit() )
   getche(); // get the key and toss it into the wind

Awesome, I can't believe I didn't think of that before, it works perfectly, the players can no longer teleport :P Thanks!

Sorry dude for hijacking your thread but I have a quite similar question.
I've got a console program waiting for user input and a friend tried to screw it up and succeeded by pasting some text from the code as input. How can I let my program proceed with just using the first line as input?

Slap your friend across the side of his head and tell him "don't do that!"

#include <stdio.h>
#include <conio.h>

int main (void) {
	int basilanTus,  /* Basılan tuş karakteri bilgisini tutacak değişken */
		devam = 1;   /* Döngü denetim değişkeni                          */
	printf("press a key \n(for exit press 'q' )\n\n");

	while (devam)  /* Sonsuz döngü */
		if (kbhit()) {  /* Bir tuşa basıldı ise */
			basilanTus = (char)getch();
			if (basilanTus == 'q')  /* Basılan karakter 'q' ise döngüden çık */
				devam = 0;
			else
				switch (basilanTus) {
				case 72 :  /* üst ok tuşu */
					printf("Upper key\n");
					basilanTus = (char)getch();			
					break;
					
				case 80 :  /*  */
					printf("lower key \n");
					basilanTus = (char)getch();			
					break;
					
				case 75 :  /* sol ok tuşu */
					printf("left key\n");
					basilanTus = (char)getch();			
					break;

				case 77 :  /* sağ ok tuşu */
					printf("right key\n");
					basilanTus = (char)getch();			
					break;
					
				default:
					printf("pressed key : %c ASCII code : %d\n", basilanTus, basilanTus);						
				}
		}
	return 0;
}

pls try this , I found this script. it gives asci value without entering key.
switch case and kbhit....

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.