Block Userinputs

Hey it's me again ;-)

I've written a little Menu like:

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
  printf("foobar");

  char ch = _getch();

  switch (ch)						
        {
	case '1':
	{
		Menu1();
		MainMenu();
	}break;
	case '2':
	{
		Menu2();
		MainMenu();
	}break;
	case '3':
	{
		Menu3();
		MainMenu();
	}break;
	case '0':
	{
	Quit();
	}


Works fine so far. Now I've got the same structure in Menu1.
'0' should be key to return to the MainMenu. But it also returns to mainMenu by any other key then '1' '2' '3'.

Is there a possibility to "block" all other inputs?
I can't tell what you're doing from the little code you've posted. Post more code.
Oh ok...sorry about that.

Here is my second Menu:
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
void Menu1()
{
	system("cls");

	printf("foobar");

	char ch1 = _getch();

	switch (ch1)
	{
	case '1':
	{
				PrepChan = 1;
				PrepareChannel();
	}break;
	case '2':
	{
				PrepChan = 2;
				PrepareChannel();
	}break;
	case '3':
	{
	}break;
	case '0': {break; }break;
	}
}


For case '0' I've set the break to return to my MainMenu. but it also returns by any other key.
Oh Blimey.
Found it by myself.
"I just forgot the default:{Menu1();}

Thanks anyways.
Do not call function recursively like that. Very stubborn user will crash it with stack overflow. Use loop here.
Sorry, but I don't really get what you mean with loops in this case :-/
Last edited on
If you need to repeat action, use loop. Not recursion. Recursion should be considered only after you have iterative solution [almost] ready.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool finished = false;
while(not finished) {
    finished = true;
    char answer = /*...*/;
    switch(answer) {
      case '1':
        //...
        break;
      case '2':
        //...
        break;
      //...
      default:
        finished = false;
    }
}
Wow...that's clever ;)
Thanks alot mate.
Topic archived. No new replies allowed.