19

I am attempting to use a simple expression such as the following and the result should be that the value of z becomes 1. However nothing seems to be happening any suggestions on how I could resolve this issue ?

template<typename t>
void MyTestB()
{

    t x = 1.0;
    t z = 0;

    std::string e = "if((x + 2) == 3){z=1;}";
    exprtk::symbol_table<t> symbol_table;
    symbol_table.add_variable("x",x);
    symbol_table.add_variable("z",z);

    exprtk::expression<t> expression;
    expression.register_symbol_table(symbol_table);


    exprtk::parser<t> parser;

    parser.compile(e,expression);
    t y = expression.value();
    std::cout << z;
}

The program does finish however at y = NAN (which is understandable because expression is a conditional statement) However z still remains 0. I was expecting it to become 1

schoetbi
  • 12,009
  • 10
  • 54
  • 72
MistyD
  • 16,373
  • 40
  • 138
  • 240
  • "nothing seems to be happening" is a somewhat vague problem description. How do you call this function? Do you get wrong outpu or no output at all? Does the program finish or hang? – us2012 Sep 18 '13 at 14:56
  • Does it work without templates? (e.g. fix `t` as `int`) – Ben Voigt Sep 18 '13 at 15:08
  • 12
    Just glancing at the examples online, it looks like your string should look like: "if((x+2) == 3, z := 1, 0)". Have you tried something like that? – Taylor Brandstetter Sep 18 '13 at 15:18
  • @Taylor yes that did the trick. Could you put that in the answe – MistyD Sep 18 '13 at 16:25

1 Answers1

19

Looking at the examples, it appears that if statements should have the form:

if (condition, expression if true, expression if false)

Also, assignment uses := instead of just =. So you should use the string:

if((x + 2) == 3, z := 1, 0)

Taylor Brandstetter
  • 3,523
  • 15
  • 24