Solved

How do you create a menu that uses an array of function pointers?

Posted on 2008-10-30
6
269 Views
Last Modified: 2013-12-14
I'm trying to create a program with a menu that uses an array of function pointers.  So when the user selects an option the appropriate function is called.  I know that I am close to figuring this out, but just haven't quite got it.  (I know that I could use a switch statement but I'm suposed to use this array.)  Please tell me what's wrong with my code.  Also is there another way that I can initialize each element in my char array to "none"?
const int size = 10;

const int length = 25;

char Name[size][length] = {"none","none","none","none","none","none","none","none","none","none"};
 

int main()

{

	int Option = 0;

		

	int (*fptr[6])(char[size][length]) = {Add, Update, Delete, Sort, Print, Quit};//declaration creating array of fuction pointers

	

	do 

	{ 

		system("cls");

		cout << "Main Menu",

		cout << "\n",

		cout << "1) Add\n",

		cout << "2) Update\n",

		cout << "3) Delete\n",

		cout << "4) Sort\n",

		cout << "5) Print\n",

		cout << "6) Quit\n" << endl;

		

		cout << "Enter Selection" << endl;

		cin >> Option;
 

		if(Option < 1 || Option > 6)

		{

			cout << "invalid entry - please re-enter" << endl;

		}		else

		{

			cout << fptr[Option - 1]() << endl;

		}
 
 

		}while(Option != 6);
 

	return 0;
 

}

Open in new window

0
Comment
Question by:b_acs
6 Comments
 
LVL 9

Expert Comment

by:mgonullu
ID: 22838911
Why you are doing in the hard way and not just use Switch statement?
Regarding the initialization of your array why not use a nested for loop
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 22838932
Hi b_acs,

I think you need to write '(*fptr[Option - 1])()' to call the functions via their pointer.

Hope that helps,

ZOPPO
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 200 total points
ID: 22838933
When you call the function through the function pointer, you need to provide the arguments. In this case you need to pass a 2D char array :

                        cout << fptr[Option - 1](Name) << endl;




>> Also is there another way that I can initialize each element in my char array to "none"?

Instead of putting "none" in there, I'd initialize the whole array to '\0' bytes, so they are all empty strings. If you want to show "none", you can always check whether the string is empty, and show "none" in that case :

        if (*name) {
            /* show name */
        }
        else {
            /* show "else" */
        }
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 53

Expert Comment

by:Infinity08
ID: 22838937
>> Why you are doing in the hard way and not just use Switch statement?

Using function pointers for this kind of stuff is cleaner. You don't need a switch.
0
 

Author Closing Comment

by:b_acs
ID: 31511568
I knew I was close!  Thank you!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22839036
Btw, instead of having something like this :

        int (*fptr[6])(char[size][length])

do yourself a favor, and make your life a little easier by using typedef's :

        typedef int (*MenuFunction)(char[size][length]);

This typedef is the function pointer as you defined it. You can then simply create an array of function pointers like this :

        MenuFunction fptr[6] = { ... };

Much easier to read and understand ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse MARs unable to start with exit code=13 2 70
Unable to open debugger port in Intellij idea 6 144
What is sub-make ? 2 39
eclipse apache tomcat admin console 39 46
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now