CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Literal command

  1. #1
    Join Date
    May 2008
    Posts
    44

    Literal command

    Hi guys I´m trying to execute a formula, for example pow(x,2), comming from an Edit1 in the main form of my program and show result in an Edit2. However I don´t know how to convert this String in a literal instruction that the program can execute;

    please suppose that we have:

    #include <math.h> ...etc

    double x=3;
    String formula = pow(x,2);
    Edit2->Text=formula;
    // This works and Edit2 shows a value of 9


    However when the String is obtained from an Edit, this is what happens:

    double x = 3;
    String formula = Edit1->Text;
    Edit2->Text=formula;
    // This doesn´t work and Edit2 shows the formula as a characters string, I mean "pow(x,2)"


    How can I get that?
    Many thank in advance

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Literal command

    Quote Originally Posted by jgabase View Post
    double x=3;
    String formula = pow(x,2);
    Edit2->Text=formula;
    What language is this? What is "String"?
    How can I get that?
    So you expected C++ to take that string and understand what it means? What "pow" means to you may mean something different to someone else. Maybe it means to draw a picture of someone socking someone in the face ("pow!").

    Seriously, there is no built-in or library function to turn a string into an expression, and then solve the expression. You have to write a function to do this (a parser), so that C++ understands what that string means and then execute that code.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2008
    Posts
    44

    Re: Literal command

    Let me explain it in other way... please imagine that I want to create the function:

    float y FUNCTION (void)
    {
    float x=3;
    y = pow(x,2); // Previously I called the header: #include <math.h>
    return y; // In this case the function will return "9" (3 to the power of 2)
    }


    Now imagine that instead of using this formula "y = pow(x,2);" I would like to use other that the user will write in an Edit component in the form of the program. What should be the procedure to get this? Is there a way for the compiler to execute a literal instruction?

    float y FUNCTION (void)
    {
    float x=3;
    ............... <--- Insert here the formula comming from an Edit I.E "y = pow(x,3);"
    return y; // In this case the function should return 27
    }


    best regards

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Literal command

    As Paul wrote you have to write a parser to accomplish this.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    May 2008
    Posts
    44

    Re: Literal command

    opss... I understand now many thanks.

Tags for this Thread

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

Featured