Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
one way to do it is to create an array of function pointers with associated strings for the name of the function. For example, support you type in "add", you would search the array for that word and when found call the associated function, something like this:
struct functions
{
std::string function_name;
void *fn();
};
functions fns[] = {
{"add", add},
{"subtract", sub},
// etc etc for each function
};
int main()
{
std::string cmd;
cout << "Enter command\n";
cin >> cmd;
// now search the array for the command and call the associated function
// not shown here.
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
> See i have a program in c or c++
Better make your mind up as to which language you're using then.
Because saying you're using C/C++ is like walking down the middle of the road blindfolded. Sooner or later, you're going to wind up being road-kill.
FWIW, the solution in C looks nothing like the solution in C++.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
you already have examples posted. use function pointers.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Perhaps function_call() can be passed a parameter, which is then used in a switch/case.
Perhaps some kind of class wrapper using operator overloading.
Perhaps taking AD's search and replacing it with std::map to do all the lookup for you.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953