I have this function in c++
double f_x(double y , string sfy )
{
return sfy;
}
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.
Something like this, perhaps:
#include <map>
#include <string>
#include <fstream>
#include <iostream>
#include <cmath>
std::map<std::string, double(*)(double)> m;
double f_x(double y , std::string sfy )
{
if (m.find(sfy) == m.end()) throw "Invalid operation.";
return m[sfy](y);
}
int main()
{
m["exp"] = std::exp;
m["cos"] = std::cos;
m["sin"] = std::sin;
std::ifstream file("test.txt");
std::string op;
std::getline(file, op);
std::cout << f_x(42.0, op);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With