|
-
Mar 2nd, 2003, 12:52 AM
#1
Thread Starter
Fanatic Member
Executing strings/char
Ok, in a program i am making, i have options, with "Name" and "Function"
The "name" is obvious, it is what is shown so the user can choose.
However, i the function is what i want the computer to do with certain variables.
I.e. say i had an option called "Add" and two variables num[0] and num[1]
the option's function is: "num[1] += num[0]; cout<<num[1]<<endl;"
How can i get the program to 'run' this string when the user selects the option, so that the variable is changed?
I.e. Do(option.function) or something like that
sql_lall 
-
Mar 2nd, 2003, 05:46 AM
#2
You can't. Since C++ is a compiled language there is no eval() function or anything like that.
What you could do is coupling function names with function pointers in a map.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 2nd, 2003, 05:47 AM
#3
You'd have to parse the string yourself, but a C++ parser is probably a bit much for a learning project
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 3rd, 2003, 03:13 AM
#4
Thread Starter
Fanatic Member
-
Mar 3rd, 2003, 05:16 AM
#5
Hyperactive Member
Re: Thanks
Originally posted by sql_lall
Ok, i'm not exactly sure what that means, but thanks anyway. I'll probably end up with a long select case call
What about the parameters then?
You would still have to parse the string.
Are you writing your own scripting language or interpreter?
-
Mar 4th, 2003, 04:28 AM
#6
Thread Starter
Fanatic Member
ok
Mainly, i just wanted something so i could make a class called "options", and could read from a .txt file what all the options should do if selected.
However, select case should do it.
Like, one option may be "Improve Weapon", then they input which weapon they want to improve. What i was hoping to do was to get a string/char array of their input, say x, and do stuff like (x.strength +=10)
So yeah, i could just loop through all the weapons, check if they are x, and if they are add to the strength.
sql_lall 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|