I tried to split string into 3 parts but its not working properly. i need it to be split by + and - and =.
int main() {
double a, b, c, x, x1, x2, d;
string str, part1, part2, part3, avand, miand, azand;
str = "2+4x-2x^2=0";
size_t count = count_if(str.begin(), str.end(), [](char c) {return c == 'x'; });
if (count == 2) {
int i = 0;
while (str[i] != '+' && str[i] != '-') {
part1 = part1 + str[i];
i++;
}
while (str[i] != '+' && str[i] != '=') {
part2 = part2 + str[i];
i++;
}
i++;
for (i; i < str.length(); i++) {
part3 = part3 + str[i];
}
}
}
std::string::find_first_ofshould be astoundingly helpful. Here's a demonstration of it in actionpart1,part2andpart3with an array or avectorlike in the demonstration because when you have sequentially named or numbered variables, using a loop and a container is almost always easier (and easier to expand when the teacher says, "Now do it with 4 parts!").+,-and=with spaces, and then simply usestd::istringstreamto get the parts. No need for tricky (and error prone) logic finding spaces, etc.