Menu with no fixed options.

The elements of a menu I'm creating are not fixed. With that I mean that some option will be available only if special conditions are met. A regular menu is easy create. You just assign a value to each option and with a Switch Statement you choose the option the user selected. In my menu, the user could see
1. Option A , 2. Option B , 3. Option C , 4. Option D
OR
1. Option A , 2. Option C , 3. Option D

Can someone direct me to a topic that presents a way to create menus like the one I want?

A solution I thought is have a unique variables(a1,a2,...) assigned to each option (starting values = 0). When the conditions for an option are met a variable x=1 is typed, followed by the Option text
1
2
3
cout << x << ". " << "Option A";
a1 = x;
x++;


1
2
3
cout << x << ". " << "Option C";
a3 = x;
x++;


That way I can have each number(1,2,..) next to an option tied to the corresponding variable and then, using a switch I can have the selected option executed.

1
2
3
4
5
cin >> choice;
switch (choice){
case a1:...
case a2:...
...


I would appreciate any thoughts about the solution I thought of or any other you have to suggest.

PS: If you find the time take a look in another question I posted some days ago.
http://www.cplusplus.com/forum/beginner/30691/
Last edited on
Something like this may help:
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <vector>
#include <string>

using namespace std;

enum Options
{
	OptionA = 0,
	OptionB = 1,
	OptionC = 2,
	OptionD = 3
};
string OptionTexts(Options opt)
{
	switch (opt)
	{
		case OptionA: return "OptionA";
		case OptionB: return "OptionB";
		case OptionC: return "OptionC";
		case OptionD: return "OptionD";
	}
	return "";
}

class Menu
{
private:
	void (*OptionAHandler)();
	void (*OptionBHandler)();
	void (*OptionCHandler)();
	void (*OptionDHandler)();
	vector<Options> active;

public:
	Menu(void (*optionAHandler)(), void (*optionBHandler)(), void (*optionCHandler)(), void (*optionDHandler)())
	{
		OptionAHandler = optionAHandler;
		OptionBHandler = optionBHandler;
		OptionCHandler = optionCHandler;
		OptionDHandler = optionDHandler;
	}
	void RunOption(Options opt)
	{
		switch (opt)
		{
		case OptionA:
			OptionAHandler();
		break;
		case OptionB:
			OptionBHandler();
		break;
		case OptionC:
			OptionCHandler();
		break;
		case OptionD:
			OptionDHandler();
		break;
		}
	}

	void AddOptionToMenu(Options opt)
	{
		active.push_back(opt);
	}

	void RemoveOptionToMenu(Options opt)
	{
		for (unsigned int i = 0; i < active.size(); i++)
		{
			if(active[i] == opt)
			{
				active.erase(active.begin() + i);
			}
		}
	}

	void PrintOptions()
	{
		for (unsigned int i = 0; i < active.size(); i++)
		{
			cout << i << ") " << OptionTexts(active[i]) << endl;
		}
	}

	void UserSellectedOption (int opt)
	{
		RunOption(active[opt]);
	}
};


This is just a fast example, you can make the number of optionHandlers variable to by putting them in a array, or something like that. And extend the Options to be classes or struct that contain there print text and handler so you can make more options at runtime and have everything in one object... But I think you get the lines of idea.
I'm still a beginner, so the class Menu has me a little confused, but I'll figure it out.
Thanks for answering.
Last edited on
Topic archived. No new replies allowed.