i have problem with using a string in a function

Hello
i have this function in c++
double f_x(double y , string sfy ) { return sfy; }

1- I want to read the string sfy from a file and pass it to the f_x function.
2- sfy contains something like y or cos(y) or exp(y).
3- when i pass y and sfy to the f_x, i want f_x calculates sfy and return it.

I can do number 1 and 3 but the problem is that the type of f_x is double and sfy is string, so i get error. I can not change the type of f_x to anything else it should be double or float.

How can i overcome this problem namely what should i do in order to f_x returns sfy as double.
Thanks
My guess would be to to pick apart the string to get the "cos" or "exp", then use this string in a switch to call the right math function, returning it's value.
Your function won't work because you're simply returning the string, not a double.

Perhaps something like this would work:
1
2
3
4
5
double f_x(double y , string sfy)
{
  if (sfy == "cos(y)") return cos(y);
  if (sfy == "exp(y)") return exp(y);
}


If you're looking for something a little more complicated so that you can pass something like "cos(y)+sqrt(sin(y))", you'll have to parse the string. That means that you'll have to identify each operation that you want to preform, ensure you write the order of operations, and figure out the resulting number form each step.
Hi
Thanks
In fact here i have brought a minimal example of my real function.
the real contents of sfy(the string) is a very large expression including cos and sin and other stuff in terms of several variables, so the little code that you have wrote is not a solution to my problem.
I do not know "parse". How can i do this? Is there any tutorial?
Topic archived. No new replies allowed.