I'm using VS 2010 and I have a problem with this part of code:
getElement returns an element at i, delElement erases elements at between (and including) i and i+1.
1 2 3 4 5 6 7 8 9 10 11 12 13
i = 0;
while(obj.getElement(i)) {
switch (obj.getElement(i)) {
case 1:
obj.setElement(/*...*/, i-1); //assigns a value to an
obj.delElement(i, i+1); //element at i-1
obj.delElement(i, i+1);
break;
// ...
default:
++i;
}
}
I think the problem is that I'm deleting an element which is the condition/statement for the loop and the switch, even though the program shouldn't perform any while or switch checks at the time. I think what happens next is that the program goes to another match, even though it should clearly perform a break - it goes to default and increments i by 1. I tested this with breakpoints and it seems like it does at least something similar. For example, the code below works, because I'm deleting and incrementing outside of the switch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i = 0;
bool flag = false;
while(obj.getElement(i)) {
switch (obj.getElement(i)) {
case 1:
obj.setElement(//...//);
flag = true;
break;
// ...
}
if (flag) {
obj.delElement(i, i+1);
obj.delElement(i, i+1);
} else ++i;
}
Is this problem a standard or am I doing something wrong?