Click here to Skip to main content
15,886,016 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C++
void tall()
{
   ....
}
void call()
{
   ....
}

typedef std::map<uint32_t, void(*)()>function;
function menu = {
	{1, [](){tall();}},
	{2, [](){call();}},
};

int main()
{
 int a;
  cout<<"choose menu [1~2]"<<endl;
  cin>>a;
   auto it= menu.begin();
	for(; it !=  menu.end(); it++) {
		if(it->first == a) {
            it->second;
            break;
		}
	}
	if(it==menu.end()){
	    cout<<a<<" menu not found"<<endl;
	}
  
	
    return 0;
}


What I have tried:

i tried above code, build succeed but not working properly.
help me.
Posted
Updated 26-Oct-20 21:52pm

Quote:
for(; it != menu.end(); it++) {
if(it->first == a) {
it->second; // <-- this should be: it->second();
break;
}

The above correction would fix your code.
Note you may write it this way
C++"
#include <iostream>
#include <map>
using namespace std;

void tall()
{
   cout << "tall\n";
}
void call()
{
   cout << "call\n";
}

using function =  std::map<uint32_t, void(*)()>;

function menu =
{
  {1, [](){tall();}},
  {2, [](){call();}},
};

int main()
{
  uint32_t a;
  cout << "choose menu [1~2]" << endl;
  cin >> a;
  auto it = menu.find(a);
  if ( it != menu.end() )
    it->second();
  else
    cout << a << " menu not found" << endl;
}


or, alternatively
C++
#include <iostream>
#include <map>
#include <functional>
using namespace std;

void tall()
{
   cout << "tall\n";
}
void call()
{
   cout << "call\n";
}

std::map<uint32_t, function<void()> > menu =
{
  {1, tall},
  {2, call},
};

int main()
{
  uint32_t a;
  cout << "choose menu [1~2]" << endl;
  cin >> a;
  auto it = menu.find(a);
  if ( it != menu.end() )
    it->second();
  else
    cout << a << " menu not found" << endl;
}
 
Share this answer
 
v2
The functions should be specified with their address with & .
C++
typedef std::map<uint32_t, void(*)()> function;
function menu = {
	{1, &tall },
	{2, &call },
};

Call the function with parenthesis like this: it->second()

The function can be called from menu directly like menu[a]() if you make sure a is either 1 or 2, in other words, within the menu boundary.

C++
#include <iostream>
#include <map>

void tall()
{
	std::cout << "tall() called\n";
}

void call()
{
	std::cout << "call() called\n";
}

typedef std::map<uint32_t, void(*)()> function;
function menu = {
	{1, &tall },
	{2, &call },
};

int main()
{
	uint32_t a;
	std::cout << "choose menu [1~2]" << std::endl;
	std::cin >> a;

	if(a >= 1 && a <= 2)
		menu[a]();
	else
	{
		std::cout << a << " menu not found" << std::endl;
	}


	return 0;
}
 
Share this answer
 
v2
Comments
CPallini 27-Oct-20 3:53am    
5.
Shao Voon Wong 27-Oct-20 5:12am    
Thanks! Your answer got my 5 as well!
CPallini 27-Oct-20 13:45pm    
So, thank you as well :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900