I’m trying to convert the expression “5 + b * 18” to “5+16.89*18”. My problem is inside the while loop. I managed to remove the spaces.
My code:
double number = 16.89;
size_t found;
ostringstream converted;
string str ("5 + b * 18");
str.erase(remove(str.begin(),str.end(),' '),str.end());
found = str.find_first_of (VALID_CHARS);
while (found != string::npos)
{
converted << fixed << setprecision (8) << number;
str.insert(found, converted.str());
found = str.find_first_of (VALID_CHARS,found + 1);
}
Can anyone help me?
Ty
insert()will shift the contents of the string to the right after the inserted text, but does not delete the character at the given position. You should usereplace(), specifying a size of ` (to replace just a single character)