Calling functions via variable

This is a discussion on Calling functions via variable within the C++ Programming forums, part of the General Programming Boards category; Ok, teaching myself C++ at the moment and have learned enought to get a text-based something or other going Anyway, ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    Calling functions via variable

    Ok, teaching myself C++ at the moment and have learned enought to get a text-based something or other going

    Anyway, Im going to issue this game commands via the console. I have already gotten them to uppercase for recognition, but now I cant seem to call my functions.

    I input my command:

    Code:
    cin >> command;
    Then I convert it:

    Code:
    for (int i = 0; i < command.size(); i++)//make the command uppercase
            {
                command[i] = toupper(command[i]);
            }
    Then I try to call my function(s), which are all defined.

    Code:
    command();
    The error: no match to call to '(std::string)'
    IDE: Bloodshed Dev C++


    I tried using the '*' for reference but still doesnt work, even though I still dont fully understand the use of the asterisk...

    Anway, Is there a simple way to get what I want done?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The simplest way would be to use a series of if, else if statements that compare the command string to a built-in string and then executes the proper function if they match.
    Code:
    if (command == "run")
      run();
    else if (command == "hide")
      hide();
    // etc.
    There are also more advanced ways like mapping the string to a function pointer.
    Last edited by Daved; 08-30-2005 at 10:38 PM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    2
    The 20 or so IF's was what I was trying to avoid, case select would work too, was trying to take the easy way

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> case select would work too
    Not with strings, only with integers or chars.

    The map version is actually not that hard, especially if your functions all have the same signature.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling functions, calculating integral
    By dakarn in forum C Programming
    Replies: 4
    Last Post: 11-18-2008, 01:14 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 04:33 PM
  3. Semi-noob Q: calling functions
    By sean... in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2004, 05:43 PM
  4. Calling App Functions from DLL
    By johnnie2 in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2003, 01:08 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21