Hi,
I'm trying to make a function wich calls other functions based on user input.
Example:
I type help and the program executes the function help.
Can this be done, if so how?
any help will be greatly apreciated
Hi,
I'm trying to make a function wich calls other functions based on user input.
Example:
I type help and the program executes the function help.
Can this be done, if so how?
any help will be greatly apreciated
To some extent, yes, this can be done. One way is to map the "command" names to the corresponding function pointers, then after reading user input, search for the name, get the corresponding key, and then call the function. If the functions may have different signatures, you probably would need a hierarchy of function objects instead if you want to make use of std::map for the mapping.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You would have to write some code that "translates" the string help to a call to the function help. In the simplest form, that would look somewhat like this:
There are other solutions that make it easier to write the code and expand the number of functions if there are many functions. But the principle is still a string comparison and a call to the relevant function.Code:char input[maxsize]; // some code to read input from the user. if (strcmp(input, "help")) { help(); }
It is IMPOSSIBLE to make C or C++ directly call a function from a string representing the name, because the name is only there in the source code. Once the compiler has processed the source-code, there is no way to know what the name of a function is.
I got sidetracked for a bit, hence the rather long time to reply.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
thanks laserlight and matsp
I'll play with both and see what suits my purpose best.
I think i'll get a long if then else statement if I go with matsp though.
Laserlights suggestion is a much more C++ solution, and I'd say that's the "proper" way to do it. Mine is the most basic (and actually not C++ at all - but it does describe the most simple solution possible). And I did say that there are better solutions if you have LOTS of functions to call.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Still, that was a C solution. The equivalent C++ solution would be:
No need to do it the hard way.Code:std::string input; // some code to read input from the user. if (input == "help") help();
A sample of laserlight's solution:
Not tested for errors.Code:// Create and initialize map typedef void (fnc_ptr)(); typedef std::map<std::string, fnc_ptr*> CommandMap_t; CommandMap_t CommandMap; CommandMap["help"] = &help; // Get input from user std::string input; // ... // Find function and call it CommandMap_t::iterator it = CommandMap.find(input); if (it) it->second(); // Call function
As I said in the previous post, my explanation was more to show the BASIC principle. It is indeed a good idea to use a map. However, the basic problem is still that SOMEHOW you need to compare a string with a constant string, and if it is a match, call the relevant function. Now, we can ACHIEVE that in many different ways.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Hi again,
I tried figuring this map thing out myself, not a big succes...
Then I tried the example Elysia provided and get the following error when compiling.
I don't know much about C++ yet, but line 23 says:Code:B:\help>g++ help.cpp -o help.exe help.cpp: In function `int main()': help.cpp:23: error: could not convert `it' to `bool'
thanks for all the help so farCode:if (it)
That is because Elysia was just giving you a rough sketch, not working code. If you want a working example:
I suggest that you read some material on how to use a std::map, e.g., cppreference.com's entry on map.Code:#include <iostream> #include <map> #include <string> // The dummy functions to call. void foo(); void bar(); typedef void (*Function)(); typedef std::map<std::string, Function> FunctionMap; // Print the list of functions. void listFunctions(std::ostream& out, const FunctionMap& function_map); int main() { using namespace std; // Create and populate the function map. FunctionMap function_map; function_map["foo"] = foo; function_map["bar"] = bar; cout << "The functions are:\n"; listFunctions(cout, function_map); string command; cout << "Which function would you like to call?\n>"; cin >> command; // Find function and call it if it exists. FunctionMap::iterator found = function_map.find(command); if (found != function_map.end()) { (*found->second)(); } else { cout << "Sorry, no function with that name exists." << endl; } } void foo() { std::cout << "You have called foo()!" << std::endl; } void bar() { std::cout << "You have called bar()!" << std::endl; } // Print the list of functions. void listFunctions(std::ostream& out, const FunctionMap& function_map) { for (FunctionMap::const_iterator iter = function_map.begin(), end = function_map.end(); iter != end; ++iter) { out << iter->first << '\n'; } }
Last edited by laserlight; 02-23-2009 at 12:29 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks, this one works.
I'm currently reading trough the reference you mentioned.
Can't make much op from it, but maybe your example will help.
Again, thank you very very much.
I understand why you want a function typedef and then declare a pointer to it, instead of the more common function pointer typedef.Originally Posted by Elysia
But then, don't call the function type 'fnc_ptr'; it's misleading.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law