Help, I don't know how to clear input when using getch()...

My code takes characters as _getch(), and I have an annoying problem that when the user enters multiple keys it executes every single one of them and so the program ( a looped menu, for example ) is not operable UNTIL it finishes with all of them.

How do I manage this? I tried using
1
2
cin.clear();
cin.ignore(10000,'\n');

...but that stops _getch() from working as it should. I would appreciate some help on this. Please let me know if I need to clarify the problem.

char option;

This is a short example of the kind of problem I am in:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
do
{

cout<<"Enter 'a' for A menu"<<endl;
cout<<"Enter 'b' for B menu"<<endl;
cout<<"Enter 'c' to exit<<endl;

option=_getch();
option=toupper(option);

if(option=='A')
 cout<<'A';
else
 cout<<'B';

}while(option!='C'); 


Problem being that my own program displays a second menu when taking input, and so if the user enters multiple letters or presses an option too many times it will carry on to the next menu and give errors until it reaches the end of the input.
Last edited on
_getch() is one of the functions from the <conio.h> header. This is non-standard, so there may not be any dependable solution as different compilers may give different results, or simply not work at all.

I'm not sure whether the problem is the repeated re-display of the menu text, which can be reduced by taking the cout outside the loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    cout << "Enter 'a' for A menu" << endl;
    cout << "Enter 'b' for B menu" << endl;
    cout << "Enter 'c' to exit"    << endl;
        
    char option; 
    do
    {        
        option = toupper(_getch());
        
        if (option=='A')
            cout << 'A';
        else
            cout << 'B';
    
    } while (option!='C'); 
  


Or if you simply want to remove surplus keypresses, you might try this:
1
2
3
4
    while (_kbhit())
    {
        _getch();
    }
Hm well the first doesn't apply, since the second menu is within a menu loop, and so if they enter multiple/numerous times on the first menu the input will carry on.

I tried the_ kbhit() loop but it makes the input go crazy, is it really this hard to limit input to one letter?
Well, I made a couple of suggestions, but perhaps I didn't fully understand the problem. Maybe your compiler behaves differently to mine (as I said, these are non-standard functions). Or maybe you need to show a more complete example of code to properly demonstrate the issue.
Well it's more like this, the reason for the gotoxy function and the array menu is that I am making a menu that highlights the current option of the user. The problem arises of course when he presses "e" or "j" too many times

As you can see, it runs actions/other functions for every option entered by the user, and the user is supposed to return to the menu to enter another option, but since he pressed a key repeatedly it continues going under the same option.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
	string menu[3];

	menu[0]="View prices";
	menu[1]="View dishes";
	menu[2]="View locations";

	char option;
	int index=0,
		xPos=5,
		yPos=5,
		yFollow=yPos;

	cout<<"Press w/s to go through options, press 'j' to choose, press e to get your daily coupon. 'c' to exit";

	do
	{

		for(int i=0;i<3;i++)
		{
			if(index==i)
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),176);
			else
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);

			gotoxy(xPos,yFollow);
			cout<<menu[i];

			yFollow++;
		}

		yFollow=yPos;

		option=_getch();
		option=toupper(option);

		if(option=='W')
			if(index!=0)
				index--;
		if(option=='S')
			if(index!=2)
				index++;

               if(option=='J')
               { 
                     if(index==1)
                        //Open certain menu
                     else if(index==2)
                         //Etc

		if(option=='E')
		{
			gotoxy(10,10);
			cout<<"You have already received your daily coupon!";
		}

	}while(option!='C');
Topic archived. No new replies allowed.