Thread: Calling functions via variable

  1. #1
    Registered User
    Join Date
    Aug 2005

    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:

    cin >> command;
    Then I convert it:

    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.

    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
    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.
    if (command == "run")
    else if (command == "hide")
    // etc.
    There are also more advanced ways like mapping the string to a function pointer.
    Last edited by Daved; 08-30-2005 at 09:38 PM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    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
    >> 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, 12:14 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Semi-noob Q: calling functions
    By sean... in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2004, 04:43 PM
  4. Calling App Functions from DLL
    By johnnie2 in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2003, 12:08 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM
Website Security Test