Subtract in a string with decrementation

closed account (GywfSL3A)
Hi,

First, here's my code :
>>>>>
std::vector<std::string> x = split("3 5", ' ');
int total = 0;
// then we loop over the elements
for(size_t i = 0; i < x.size(); ++i) {
// convert the string to an integer
int n = atoi(x[i].c_str());
total = total + n;
}

std::cout << "total = " << total << std::endl;
}
<<<<<

So, as you can see, this will add 3 to 5. However, I would like it to do the inverse (3 - 5).

How I can do that?
Thank you for your help.
Last edited on
Will there always be two elements? What do you expect to happen with three or more elements in subtraction mode? Why does the first element get special treatment?
Last edited on
It is not uncommon for (- 3 5 7) to mean (3 - 5 - 7), so there is no problem there.
Topic archived. No new replies allowed.