0

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?

Korra
  • 1
  • 1
  • 1
    `stop(number)` does what exactly again? Post a [mcve] that exactly reproduces your problem as required here please. – πάντα ῥεῖ Mar 30 '19 at 15:29
  • it is suppose to be stoi. I fixed it. autocorrect sorry. – Korra Mar 30 '19 at 15:35
  • 1
    C++ does not work this way. The only thing you can "import" from a file is text strings. You will need to write all the code that translates whatever you're imported into whatever your program needs to use. Unfortunately, there is no built-in library function in C++ that works this way. C++ is a compiled language, not an interpreted language. – Sam Varshavchik Mar 30 '19 at 15:39
  • In addition to Sam: I see two solutions. 1. the easy to implement: You provide a fixed set of parameters which can be given in text file e.g. in the form of `fRGV8=23.7`. Then you need a simple reader which reads these params into variables which are already part of your C++ program. With default values for variables, it wouldn't hurt if a param is missing in text. (It's just like ini files.) 2. the sophisticated way: [SO: Parsing strings of user input using the cparse library from git](https://stackoverflow.com/a/46965151/7478597) (I didn't use `cparse` but that would be another option.) – Scheff's Cat Mar 30 '19 at 15:46
  • You may like to have a look at a basic calculator parser and evaluator in boost spirit: https://www.boost.org/doc/libs/1_65_1/libs/spirit/example/qi/calc_utree.cpp – Maxim Egorushkin Mar 30 '19 at 16:19
  • Define the variables using an xml file. It will be a lot easier to parse/edit later from c++. – seccpur Mar 30 '19 at 16:19
  • Sounds like you want to write a parser. – Jesper Juhl Mar 30 '19 at 17:03

0 Answers0