String

This is a discussion on String within the C++ Programming forums, part of the General Programming Boards category; If I do a declaration like this, this will work: int Number = 5; My question now is this. If ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    String

    If I do a declaration like this, this will work:

    int Number = 5;


    My question now is this.

    If you have this declaration in a string like below (Example).
    Is there any possible way to convert this string so it will
    function like the statement above.
    Is there Any possible way to do this because actually the declaration is
    within the "" ?

    std::string Example;

    Example = "int Number = 5;"

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No. C++ is not a dynamic language.

    You can write an interpreter for some expression language and run its expression, but you'll have to register every variable you want to modify with it explicitly. Or you can modify the interpreter's internal variables, but they won't be C++ variables.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    If you do this in C# instead. Is it possible to do here ?
    Is is kind of confusing for me.

    From what I have understand there is a difference between C# and C++ here.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    C# isn't an interpreted language either. The trouble is that, for this to work, the compiler would have to know the value of the string, but the value of the string isn't known until the program actually runs.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Okay. The thing is that for the application that I am doing. It is a Windows form application.

    If I take this example. When using this application, one must have the possibility to change a line of code that is written.
    Let us say an example:
    Code:
    int Value1 = 2;
    int Value2 = 3;
    
    if Value1 < Value2 etc..
    This line will be possible to change to ex: Value1 > Value2.
    This is one very important piece with my program.
    There must be a way I beleive. To recompile the program or whatever possible from within the application or use an attachment file that will be read from.
    I will be open for every possilbe way to do this, even that it isn&#180;t the best way to do it etc...

    I really dont know where to begin here.
    Last edited by Coding; 02-03-2008 at 01:43 PM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    How about
    Code:
    if( flag && Value1 < Value2 || !flag && Value1 > Value2)
    ?

    What's your real use case?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I am not sure how you meen with flag and !flag (flag or not flag)

    This was only an example though. I will take a more complex example what the use will be with this:

    Code:
    int Number1 = 5;
    int Number2 = 4;
    int Number3 = 3;
    int Number4 = 2;
    
    if (Number1 > Number2 && Number 2 < Number 3 &&  //These 2 lines will be possible to change
    Number 4 == Number 1)
    The thing is that I can be able to write these 2 lines from within the Windows Form application in any way
    Then the real code that contains the variables int Number1 etc.. will recognice the inputs/statement that I have done.
    This is an example of a combination. It could be endless of combinations for the statement.

    Is there any way to use pointers to variables in the code so it will be recogniced... Any step by step method so what I will write in the statement till be recogniced in the code

    So for this case I use the different operators, &&, >, < and == and variables: Number1 to Number4
    Last edited by Coding; 02-03-2008 at 03:20 PM.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    For example if I write a string like: "Number1"
    Can this be converted back to just: Number1 wich will be recogniced by a already existed variable that has been declard to:

    int Number1 = 5;

    To use a pointer in any way ?
    Last edited by Coding; 02-03-2008 at 03:22 PM.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Why not just

    • parse the string "int Number = 5"
    • create a new int variable in some malloc'ed storage
    • cast and assign an int 5 to it
    • have another array of structures, in which the structure is basically a char array assigned "Number" (your symbol) and a pointer to the malloc'ed storage with the new int

    That's (essentially) how scripting languages work that are written in C (like Ruby).

    Todd

    (edit - I wasn't paying attention to the forum. Substitute vectors and strings and other C++ terms/stuff for C terms/stuff!)
    Last edited by Dino; 02-03-2008 at 03:38 PM. Reason: more into
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    This sounds really interesting. It sounds like a solution but diffus in my mind for the moment. Just a lot to take in on the same time.
    Just a few quesions though to put things in place to understand.

    If I only do this, I will write this string: "Number" can this directly point to the already existing
    int Number = 5; (wich is Number when you refer to it in code, so "Number" could point to Number ?)

    Can I point "Number" directly in any way or do I have to do like you described.
    Last edited by Coding; 02-03-2008 at 04:01 PM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You'll need some sort of structure, such as:
    Code:
    struct variable
    {
       std::string name;     // "Number" in the above case.
       enum { IntType, DoubleType, StringType } type;
       union {
          int i;
          double d;
          std::string str;
       } value;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Thanks I think this will be a good start for me to look at.

    I will read about pointers how they work exactly to continue.

    Coding

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21