Hi!

I have 5 different functions which all must be connected to main, for example a menu that calls each function. How do I do this?

Thanks in advance!

Best regards
Adam

Recommended Answers

All 5 Replies

in main(), do as following:

int choice = menu();/*implement menu. It should returns the user choice*/
switch(choice)
{
   case 1:
          fun1();/*your fun*/
          break;
   case 2:
          fun2();/*your fun*/
          break;
    /*some more cases as required*/
   default:
         /*some error message*/
}

Hi again!

Thanks, I am going to try this at once!

One more question is, if my fifth function must returns a value in main, how would I do this? For example a sum from two numbers?

Thanks in advance!

Best regards
Adam

case 1:
	int newValue == fun2(number1, number2);
	break;

my qustion is why u use switch and break statments ????/

kindly explain

it's similar to using multiple if/else statements but is more efficient when the value you are checking has only one option, ie making a selection from a menu.

Check this

// Enumeration for menu selection
enum Menu {
	ADDTOEND = '1',
	ADDTOSTART = '2',
	ADDSPECIFIC = '3',
	REMOVESPECIFIC = '4',
	PRINTLIST = '5',
	QUIT = '9',
};


int menuSelection = 0;

	while(menuSelection != QUIT)
	{
		menuSelection = SelectMenuChoice();


		switch(menuSelection)
		{
		case ADDTOEND:
			AddToEnd(userList);
			break;
		case ADDTOSTART:
			AddToStart(userList);
			break;
		case ADDSPECIFIC:
			AddSpecific(userList);
			break;
		case REMOVESPECIFIC:
			if(userList->head != 0)
			{ RemoveSpecific(userList); }
			else
			{ cout << "No nodes to delete" << endl; }
			break;
		case PRINTLIST:
			PrintList(userList);
			break;
		default:
			break;

		}
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.