Ok, so I am working on a function that remove a element from the array if the name entered by a user is = to a element in the array
It clears of the memory of said element but i need to re order the array
I searched on here but nothing seemed to cover it for an array of structs
for(int i = 0; i < lCount; i++) { if(name == songs[i].title) { songs[i].title.clear(); songs[i].artist.clear(); songs[i].genre.clear(); songs[i].time = 0; songs[i-1].title = songs[i].title; songs[i-1].artist = songs[i].artist; songs[i-1].genre = songs[i].genre; songs[i-1].time = songs[i].time; }
Close, but not quite. You just overwrote songs[i-1], which presumably may NOT be a duplicate. I assume you want to "delete" songs[i], by overwriting it with songs[i+1], like this:
for(int i = 0; i < lCount; i++)
{
if(name == songs[i].title)
{
songs[i].title = songs[i+1].title;
songs[i].artist = songs[i+1].artist;
songs[i].genre = songs[i+1].genre;
songs[i].time = songs[i+1].time;
}
}
This isn't quite the solution. There are still problems with it and thus you need to do more, but you're no longer overwriting a song that wasn't a duplicate, so it's a start.
Note that the word "delete" is in quotes on purpose. Depending on how technical one wants to be, you're not really "deleting" or "removing" anything from the array. The array stays the same size. But you're changing the array in an organized way such that you have one fewer element that you care about (i.e. a value may still be there, but you couldn't care less what it is), so in effect you're deleting it.
Not sure if the above paragraph adds or detracts from the discussion. If it detracts, just ignore it for now. :)