| | |
Removing element from array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 141
Reputation:
Solved Threads: 3
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
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
C++ Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Jan 2008
Posts: 3,831
Reputation:
Solved Threads: 501
•
•
•
•
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
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Feb 2009
Posts: 141
Reputation:
Solved Threads: 3
Well in this particular function, as seen in my previous post, you can recall the code
I call this function to ask the user to input a song title, then I sort through the array to see if there is a corresponding element in the array and remove that item from the array, then I "shorten" the array so It can print out the array properly when I call my print function, hence why I used
I thought it would clear out the memory, which I did, but I failed to shift the array.
However, you code make much more sense to simply shift the index of the array over 1 so that value gets overwrited if the user inputs a title that corresponds to an element in the array
Just as an FYI the user input is...asking the user to input the title of a song only.
As you said its it not the final solution because
(1,2,3,4) example of output
now after said "removal"
(1,2,4,4)
I just need to learn how to remove that duplicated element which seems to be the following element after the match
in titles
And I think I fixed it, I am bit abrupt on explaining my meaning but I did this to the "remove" function
and when I call this function I simple did
lCount = remove(songs, lCount);
So the iterator of "lCount"; which the current arrays size depending on the last file read gets reduced by 1
Not gonna lie it is a buggy in some instances, the remove and then print correct amount of array works some times the value does repeat
I call this function to ask the user to input a song title, then I sort through the array to see if there is a corresponding element in the array and remove that item from the array, then I "shorten" the array so It can print out the array properly when I call my print function, hence why I used
C++ Syntax (Toggle Plain Text)
songs[i].title.clear(); songs[i].artist.clear(); songs[i].genre.clear(); songs[i].time = 0;
I thought it would clear out the memory, which I did, but I failed to shift the array.
However, you code make much more sense to simply shift the index of the array over 1 so that value gets overwrited if the user inputs a title that corresponds to an element in the array
Just as an FYI the user input is...asking the user to input the title of a song only.
As you said its it not the final solution because
(1,2,3,4) example of output
now after said "removal"
(1,2,4,4)
I just need to learn how to remove that duplicated element which seems to be the following element after the match
in titles
And I think I fixed it, I am bit abrupt on explaining my meaning but I did this to the "remove" function
C++ Syntax (Toggle Plain Text)
int remove(music songs[10], int lCount) { string name; int i =0; cout<<"Enter title: "; cin.ignore(); getline(cin,name); 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; } } return (lCount - 1); }
and when I call this function I simple did
lCount = remove(songs, lCount);
So the iterator of "lCount"; which the current arrays size depending on the last file read gets reduced by 1
Not gonna lie it is a buggy in some instances, the remove and then print correct amount of array works some times the value does repeat
Last edited by power_computer; Sep 2nd, 2009 at 1:45 am.
struct can be easily copied using an assingment operator.
You can't remove an element from the an array. However you may assign an empty struct,
C++ Syntax (Toggle Plain Text)
songs[0]=songs[1];
C++ Syntax (Toggle Plain Text)
music empty={"","","",0}; songs[i]=empty;
You're on the right track, but not really moving all the songs following the deleted item up. You only move the i+1 element to the i postion when the match was found. Everything past that will remain where it was.
That is, if the list is 1 2 3 4 5 6 and you want to remove 3, you'll get 1 2 4 4 5 6, but with the reduced count, item 6 won't be seen.
Better method is to iterate through the array till you find the item to remove, or run out of array (it's not found). When you find it, begin a loop that moves every remaining item up one position, starting from the location item was found to the end of array.
Oh - watch for reading past end of array. If your loop counter i points to the last element, what's at i+1?
Or, using the code you've written, use the comparison to set a flag if match is found, and use that flag to indicate if an item should be moved up.
That is, if the list is 1 2 3 4 5 6 and you want to remove 3, you'll get 1 2 4 4 5 6, but with the reduced count, item 6 won't be seen.
Better method is to iterate through the array till you find the item to remove, or run out of array (it's not found). When you find it, begin a loop that moves every remaining item up one position, starting from the location item was found to the end of array.
Oh - watch for reading past end of array. If your loop counter i points to the last element, what's at i+1?
Or, using the code you've written, use the comparison to set a flag if match is found, and use that flag to indicate if an item should be moved up.
C++ Syntax (Toggle Plain Text)
bool move = false; for(int i = 0; i < lCount; i++) { if(name == songs[i].title) { move = true; //never gets reset to false continue; //we'll update on the next loop pass } if( move ) { 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; } }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Feb 2009
Posts: 141
Reputation:
Solved Threads: 3
Your method works when i+1, i-1 doesnt seem work as well, however, as stated i+1 is still a bit buggy in some circumstances, it seems the boolean is checking well, because when enter anyname the next value in the array is removed and not displayed, hmmm
Last edited by power_computer; Sep 2nd, 2009 at 2:08 am.
•
•
•
•
Your method works when i+1, i-1 doesnt seem work as well, however, as stated i+1 is still a bit buggy in some circumstances
continue statement as shown.Also, your decrementing of the count that's returned should only occur when the flag "move" is true. Don't modify it if the target song was not found.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Feb 2009
Posts: 141
Reputation:
Solved Threads: 3
It is still buggy, not removing properly when I imply your method
Here is my entire code
The read and print functions work fine as far I tested them with 3 different files, and also the read function as to read from a song.dat file upon start up just wanted to start that
Here is my entire code
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; struct music { string title; string artist; string genre; int time ; }; int read(char* file, music songs[10] , int lCount = 0); void print(music songs[10], int lCount); int remove(music songs[10], int lCount); int main() { music songs[10]; int j = 0, lCount = 0, choice = 0; char file[256]; lCount = read("song.dat",songs); do { cout<<endl; cout<<"Enter choice: "; cin>>choice; switch(choice) { case 1: cout<<"Enter new file: "; cin>>file; lCount = read(file, songs, lCount); break; case 2: print(songs, lCount); break; case 3: lCount = remove(songs, lCount); break; default: cout<<" "; break; } }while(choice != 100); system("Pause"); return 0; } int read(char *file, music songs[10], int lCount) { ifstream fin; string string; int i = 0; for(int j=0; j < lCount; j++) { songs[i].title.clear(); songs[i].artist.clear(); songs[i].genre.clear(); songs[i].time = 0; } fin.open(file); while(!fin.eof() && i < 10) { getline(fin,songs[i].title); getline(fin,songs[i].artist); getline(fin,songs[i].genre); getline(fin,string); songs[i].time = atoi(string.c_str()); i++; } fin.close(); return(i); } void print(music songs[10], int lCount) { for(int j = 0; j < lCount; j++) { cout<<songs[j].title <<songs[j].artist <<songs[j].genre <<songs[j].time<<endl; } } int remove(music songs[10], int lCount) { string name; int i =0; bool move = false; cout<<"Enter title: "; cin.ignore(); getline(cin,name); for(int i = 0; i < lCount; i++) { if(name == songs[i].title) { move = true; continue; } if(move) { 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; } } return (lCount); }
The read and print functions work fine as far I tested them with 3 different files, and also the read function as to read from a song.dat file upon start up just wanted to start that
You got the assignment backwards, should be
C++ Syntax (Toggle Plain Text)
songs[i-1].title = songs[i].title; //etc.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Find the biggest element in that array (C++)
- Ignore duplicate element in array and continue reading (Java)
- how to add the first element of first array of every record (Visual Basic 4 / 5 / 6)
- Comparing an element in an array (C++)
- removing an element... (C)
- Permutations of an N element array (C++)
- Stripping the last element off an array (Perl)
- inserting an element into an array in c language (C)
- inserting an element into an array in c language (C)
Other Threads in the C++ Forum
- Previous Thread: My push_back keeps going into an eternal loop
- Next Thread: How to update a vector? ie delete elements after accessing
| Thread Tools | Search this Thread |
api application array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll email encryption error file format forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream image input int integer java lib linux loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct studio template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






