Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

str.replace() brings up an assertion error.

$
0
0

I have been scripting a free form calculator using c++ for one of my assignments and i have been having trouble with str.replace() to replace an operation in the original input string to a calculated value (in this case i input 4^0.5= and it calculates 4^0.5 which is 2 then replaces 4^0.5 with 2 and then moves onto the next operation) however when i am trying to replace it it keeps on bringing up an assertion error. This is the small bit of code which works out the power (after parsing the double values out of the string) then converts the resultant double value from pow into a string and then goes to replace it in the input string (inp).

double po = pow(num,power); stringstream ss; ss << "" << po << ""; int s = (startpos < 0) ? 0 : startpos; int e = ((unsigned int)((endpos + 1) - startpos) > inp.length()) ? inp.length() : ((endpos + 1) - startpos); string rep = inp.substr(startpos, ((endpos + 1) - startpos)); cout << "rep:" << rep << ":" << endl;

cout << "init input:" << inp << endl;

inp.replace(s, e, ss.str()); cout << "final input:" << inp << endl;

The error that comes up is this the fun part is that is has replaced the string and this error does not come up if i comment out the inp.replace line:



 

Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>