Using a variable literally

Discussion in 'C++' started by David Hutto, Dec 31, 2010.

  1. David Hutto

    David Hutto Guest

    In the following I would like to know how I can convert the cin(in
    getline, or using cin directly) for function into a type that uses the
    variable 'function', with an actual call to the function display?

    So if I enter display ("string here") at the cin prompt it will call
    display() and cout string a.

    #include <iostream>
    #include <string>
    using namespace std;

    int display (string a)
    {cout << a;}

    int main()
    {
    string function;

    cout << "< ";
    getline(cin,function);
    function;



    return 0;
    }


    Thanks,
    David
     
    David Hutto, Dec 31, 2010
    #1
    1. Advertising

  2. On 31/12/2010 4:30, David Hutto wrote:
    > In the following I would like to know how I can convert the cin(in
    > getline, or using cin directly) for function into a type that uses the
    > variable 'function', with an actual call to the function display?
    >
    > So if I enter display ("string here") at the cin prompt it will call
    > display() and cout string a.
    >
    > #include<iostream>
    > #include<string>
    > using namespace std;
    >
    > int display (string a)
    > {cout<< a;}
    >
    > int main()
    > {
    > string function;
    >
    > cout<< "< ";
    > getline(cin,function);
    > function;
    >
    >
    >
    > return 0;
    > }
    >
    >
    > Thanks,
    > David


    There is no simple and elegant way to do this in C++ (that I know of),
    for the simple reason that C++ is a compiled language, not an
    interpreted one like perl, python or lua.

    One option is to maintain a mapping of strings to function pointers,
    that is, something like std::map<std::string,int (*)(std::string)>.
    Then, you can look up which function corresponds to the one provided by
    the user, and call it.
    This method has some obvious drawbacks, one of which is that all of the
    functions that can be 'called' by the user must have the same method
    signature.

    The second option is to embed a scripting language in your program. Lua
    and python both provide hooks that allow you to embed the scripting
    language in your C++ program, and to allow commands in that scripting
    language to call your C++ functions.
    For more information, see:

    http://docs.python.org/c-api/
    http://www.lua.org/manual/5.1/manual.html#3

    HTH,
    Frederik
     
    Frederik Van Bogaert, Dec 31, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kentor
    Replies:
    47
    Views:
    1,233
    derbz
    Feb 16, 2007
  2. Replies:
    7
    Views:
    334
    CBFalconer
    Nov 5, 2006
  3. Replies:
    5
    Views:
    604
  4. Eric Smith

    function to return its argument literally

    Eric Smith, Nov 15, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    89
    Malte Ubl
    Nov 17, 2003
  5. Suk
    Replies:
    2
    Views:
    348
    John Bokma
    Jan 22, 2007
Loading...

Share This Page