### Convert string to equation ADT

I'm writing a function to convert a string to an equation ADT which has got two members: "grado" that is the grade of the equation and "coefficienti" that is a vector of int containing the coefficients of the equation.

For example: "9x^3-2x" ---> grado = 3, coefficienti = {0, 2, 0, 3}.

This is my code. It works correctly till the last for-loop where it only does one loop and then gives me a "SIGABRT" error.

Does anybody know how to fix it?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````equazione convEquaz(string str) { equazione e; if (str.find_first_of("xX") == str.npos) { e.grado = 0; e.coefficienti.push_back(atof(str.c_str())); } else { if (str.find('^') == str.npos) { e.grado = 1; if (str.find_first_of("+-") == str.npos) { e.coefficienti.push_back(0); str.erase(str.find_first_of("xX")); e.coefficienti.push_back(atof(str.c_str())); } else { string str2 = str.substr(str.find_first_of("+-")+1); e.coefficienti.push_back(atof(str2.c_str())); str.erase(str.find_first_of("xX")); e.coefficienti.push_back(atof(str.c_str())); } } else { string str2(str.begin()+str.find('^')+1, str.begin()+str.find_first_of("+-")); e.grado = atof(str2.c_str()); e.coefficienti.resize(e.grado+1); for (int i = 0; i < e.grado+1; i++) { string monomioFinale = str.substr(str.find_last_of("+-")); if (monomioFinale.find_first_of("xX") == monomioFinale.npos) { e.coefficienti[0] = atof(monomioFinale.c_str()); } else if (monomioFinale.find('^') == monomioFinale.npos) { string str2 = monomioFinale.substr(0, monomioFinale.find_first_of("xX")); e.coefficienti[1] = atof(str2.c_str()); } else { string str2 = monomioFinale.substr(0, monomioFinale.find_first_of("xX")); string str3 = monomioFinale.substr(monomioFinale.find('^')+1); int index = atoi(str3.c_str()); e.coefficienti[index] = atof(str2.c_str()); } str.erase(str.find_last_of("+-")); } } } e.minimiTermini(); return e; }``````
You're calling:
 `` `` ``string monomioFinale = str.substr(str.find_last_of("+-"));``
where str is
 `9x^3`
Thank you, kbw, you've been very helpful.
Topic archived. No new replies allowed.