Results 1 to 6 of 6

Thread: Executing strings/char

  1. #1

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking 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

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  4. #4

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking Thanks

    Ok, i'm not exactly sure what that means, but thanks anyway. I'll probably end up with a long select case call
    sql_lall

  5. #5
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396

    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?

  6. #6

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking 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
  •  



Click Here to Expand Forum to Full Width