Evaluating math inside of a string C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Evaluating math inside of a string C++

I'm working on something and I'm wondering if I have a string "2+2" or "pow(2,5)" (and I have included math.h), is there is any direct to convert that into 4 and 32 respectively? I can convert "4" into a double but it won't work with operators. If there isn't any direct way, I have a work-around but it'll be a pain to implement so I'd rather not if I don't have to.

9th Dec 2017, 7:23 PM
David MacDonald
David MacDonald - avatar
3 Answers
+ 2
No. You have to parse the string.
9th Dec 2017, 7:26 PM
1of3
1of3 - avatar
+ 2
I looked into this for you and 1of3 is correct. The only way is possible by embedding another scripting language like Lua or Java.Which would then let you execute the strings code.
10th Dec 2017, 4:48 AM
Michael Simnitt
Michael Simnitt - avatar
+ 1
Okay, that's what I was afraid of lol but ty for the answer
9th Dec 2017, 7:27 PM
David MacDonald
David MacDonald - avatar