In my project i need to take an expression input from user as string and then show the result of the expression. I am taking the input as follows
char address[100] = {NULL};
fgets(address, 100, stdin); //take the expression as input here
say user wrote the expression as
address = " 1+5+9" .
What i need to do is to show the result of the expression. I can do this by separating each number and then doing the calculation. But i just wanted to know is there any better way to do this?
It’s sounds like you’re looking for a C++ equivalent to something like
evalin JavaScript. Unfortunately, C++ doesn’t have one. Parsing the expression by hand is the best you can do.If you have access to The C++ Programming Language book, chapter 6 covers how one would write a simple calculator program, solving the same problem you have.