Implementing a menu class: is it possible and how do I do it?

Hi everyone,

Basically, I'm looking to create a class that allows me to set up multiple menus. As it is now, my code uses several functions like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void subMenuTwo()
{
	char select;
	while (true)
	{
		// User menu
		cout << "Select from the following items:" << endl <<
			"H: Go to hfunc" << endl <<
			"E: Go to efunc" << endl <<
			"X: Exit" << endl;
		cin >> select;

		switch (select)
		{
		case 'H': hfunc();
			break;
		case 'E': efunc();
			break;
		case 'X': mainMenu();
		}
	}
}


Essentially, the program opens the main menu, the user chooses an option, then they are taken to a submenu where they have more options like the above. I was wondering whether it'd be possible to make a Menu class to where I can simply declare a new instance of Menu when I need to make a submenu, as opposed to having a mainMenu() and 3 or 4 subMenu() that all basically do the same thing.

Here is my horrific failed implementtion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Menu {
	char select;
	string options[];
	public:
		void menu()
		{
			while(true)
			{
				cout << "Select from:" << endl;
				cin >> select;
				for(int i=0; i<=sizeof(options); i++)
				{
					if(select==options[i][0])
						options[i][1]();
				}
			}
		}
};

Well, you can have functions inside functions, so technically I dont see why you couldnt include a class within a class ? Ive never really worked with classes before......Ill see if i can find something

try these links:

1. http://www.cplusplus.com/forum/general/37926/

2. http://www.cplusplus.com/forum/beginner/73054/
Last edited on
You cannot declare functions in other functions, only in global scope, but you can declare class in another class (if I remember correctly :)
Topic archived. No new replies allowed.