So I basically have a mini dictionary of user defined variables. What my code basically does is take a txt file generated by the user and does some mathematical calculations. What I need to do is get the input from the txt file and calculate the equation.
I have tried to import it as a string and separate it to later multiply the 2 variables together. I then try to change them to doubles using static cast. However since its a user defined string the program keeps giving me errors.
how I declare my defined variable:
define RG8F 108.9873216748352845
how it appears in the txt file:
RG8F*23.7
how I import it:
string line;
ifstream file("path\to\file")
getline(file, line)
input.result= evaluate(line);
evaluate function (first try):
double evaluate(string exp)
{
size_t sign=exp.find('*');
string number = exp.substr(0,sign);
string type = exp.substr(sign+1)
double result = static_cast<double>(stoi(number));
double type2 = static_cast<double>(stoi(type));
return result*type2;
}
error: Invalid_argument at memory allocation
evaluate function (second try):
double evaluate(string exp)
{
size_t sign=exp.find('*');
string number = exp.substr(0,sign);
string type = exp.substr(sign+1)
double result = static_cast<double>(stoi(number));
return result*type;
}
error: No operator * matches these operands
evaluate function (third try):
double evaluate(string exp)
{
size_t sign=exp.find('*');
string number = exp.substr(0,sign);
string type = exp.substr(sign+1)
double type2 = 0;
double result = static_cast<double>(stoi(number));
if (type == "RG8F")
type2 = RG8F
return result*type2;
}
Problem: I have a library filled with user-defined variables and don't want to if every single one since it would take forever. Is there a way to use the user-defined variable instead of using an if for each one?