Deleting from an array

Hi all, my array has a max size of 10 places; and it contains a string, Im trying to delete a letter chosen by the user. It almost works just my problem is if there are 2 letters next to each other in which it only removes one of them.
Any help is useful thanks

1
2
3
4
5
6
  	cout << "Looking for " << letter << "...";
	
	for (int pos = 0; table[pos] != EOT; ++pos)
		if ((table[pos] == letter) || (table[pos] == toupper(letter)))
			for (int i = pos; table[i] != EOT; ++i){
					table[i] = table[i + 1]; }
You could look at the documentation of std::remove on this site. It does almost, but not quite, the same job.
Your problem is that when you delete the first letter, you're doing two things:
1) You're moving the subsequent characters down one position in the array. The second occurrence is now in table[pos].
2) You increment pos as part of your for loop. This is what is causing you to skip over the second character.

If you delete a character, you don't want to increment pos for that iteration of the loop, or you'll skip over the second occurrence.

Consider a while loop instead. That will give you more control when you want to increment pos.


Topic archived. No new replies allowed.