Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using a variable literally

Reply
Thread Tools

Using a variable literally

 
 
David Hutto
Guest
Posts: n/a
 
      12-31-2010
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
 
Reply With Quote
 
 
 
 
Frederik Van Bogaert
Guest
Posts: n/a
 
      12-31-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
15 seconds survey (literally) 1 radio button click. thx for your time Kentor HTML 47 02-16-2007 03:58 AM
Binary output (literally 1's and 0's) noridotjabi@gmail.com C Programming 7 11-05-2006 12:28 AM
Spielberg re-inventing the cinema experience - literally RobMac DVD Video 2 10-12-2005 11:15 PM
The Definitive Chord & Scale Bible - Literally EVERY chord and scale! Kind of Blue2 Digital Photography 1 02-08-2005 10:14 PM
EVERY Chord and Scale for guitar- LITERALLY!! Music Rises Above Digital Photography 0 01-27-2005 11:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57