I want to erase the last element from the vector.
I then do vec.erase(vec.end() - 1) . How ever, the program crashes at this level.
here is my code.
1 2 3 4 5 6 7 8 9 10 11
while(next)
{
for(int i = 0; i < prime.size(); i++)
sum += prime.at(i);
if(sum < max && isPrime(sum))
next = false;
else
prime.erase(prime.end() - 1); //still crash if i change 1 to any other positive number
//still crash if i use prime.erase(prime.begin() + x) ,x = any positive number
}
yes, it is empty. Set a conditional breakpoint and you may observe it, after that is undefined behaviour.
In line 28 you ask for `sum' to be less than `max', that's impossible. Perhaps you forgot to reset `sum', or the condition is another one, ¿what are you trying to solve?
Also, you consider 0 and 1 to be primes (they are not)
Because sum was not being reset, your condition if(sum < max && isPrime(sum)) was always false after some point. This caused your else condition to then always be hit, thus infinitely trying to remove the last element.