Quick Question.. Menu Navigation?

I am creating a program that has a menu used for navigating around it to different function and whatnot. I was wondering if there is a way to make it so that instead of typing in a number and pressing enter, you could just press the number associated with the next "page" to activate the function within it. I am very much a newbie, this is my first year practicing with C++, and understand if this takes a lot of work, but I am also just looking for some general tips on making an easy method navigation instead of "if user enters 1 go here, if user enters 2, go there, etc".
Just cause I'm bored... I'm not going to comment it though. If you don't know anything, Google (: lol

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
// Press to select

#include <iostream>
#include <conio.h>

bool getKeyPressed( char &key )
{
	if( _kbhit() )
	{
		key = _getch();
		return true;
	}

	return false;
}

int main()
{
	char key = ' ';
	bool running = true;

	std::cout << "\'q\' to quit.\n";

	do
	{
		if( getKeyPressed( key ) )
		{
			switch( key )
			{
				case '1':
					std::cout << "You pressed one.\n";
					break;

				case '2':
					std::cout << "You pressed 2.\n";
					break;

				case 'q':
					std::cout << "Bye-bye\n";
					running = false;
					break;

				default:
					std::cout << key << " is invalid entry.\n";
					break;
			}
		}
	}while( running );

	return 0;
}
Wow that was fast, lol thanks! I'll have to go through this and try to figure out how this works. Thanks again.
No worries. Have fun! lol
I've been playing around with this and I need help with two problems I'm running into.
For one, I can't access the functions. I set it up so that when the key is pressed a function will run but it just remains blank.
Secondly, I can't really figure out how I would return to the main menu after reaching said function. Any suggestions? I don't mean to be a bother, I'm just extremely confused lol.
You put the function calls within the case statements! Replace line 31 etc with you call to your function.

As for returning to this, when the function you call ends, it will return to this do-while loop. So long as the user doesn't press 'q', it will keep running.

i.e. This is your main menu.
Last edited on
Topic archived. No new replies allowed.