943,800 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2033
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 2nd, 2009
0

Removing element from array

Expand Post »
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)
  1. for(int i = 0; i < lCount; i++)
  2. {
  3. if(name == songs[i].title)
  4. {
  5. songs[i].title.clear();
  6. songs[i].artist.clear();
  7. songs[i].genre.clear();
  8. songs[i].time = 0;
  9.  
  10. songs[i-1].title = songs[i].title;
  11. songs[i-1].artist = songs[i].artist;
  12. songs[i-1].genre = songs[i].genre;
  13. songs[i-1].time = songs[i].time;
  14. }
Similar Threads
Reputation Points: 30
Solved Threads: 3
Junior Poster
power_computer is offline Offline
144 posts
since Feb 2009
Sep 2nd, 2009
0

Re: Removing element from array

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)
  1. for(int i = 0; i < lCount; i++)
  2. {
  3. if(name == songs[i].title)
  4. {
  5. songs[i].title.clear();
  6. songs[i].artist.clear();
  7. songs[i].genre.clear();
  8. songs[i].time = 0;
  9.  
  10. songs[i-1].title = songs[i].title;
  11. songs[i-1].artist = songs[i].artist;
  12. songs[i-1].genre = songs[i].genre;
  13. songs[i-1].time = songs[i].time;
  14. }

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)
  1. for(int i = 0; i < lCount; i++)
  2. {
  3. if(name == songs[i].title)
  4. {
  5. songs[i].title = songs[i+1].title;
  6. songs[i].artist = songs[i+1].artist;
  7. songs[i].genre = songs[i+1].genre;
  8. songs[i].time = songs[i+1].time;
  9. }
  10. }

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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Sep 2nd, 2009
0

Re: Removing element from array

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

C++ Syntax (Toggle Plain Text)
  1. songs[i].title.clear();
  2. songs[i].artist.clear();
  3. songs[i].genre.clear();
  4. 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)
  1. int remove(music songs[10], int lCount)
  2. {
  3. string name;
  4. int i =0;
  5.  
  6. cout<<"Enter title: ";
  7. cin.ignore();
  8. getline(cin,name);
  9.  
  10. for(int i = 0; i < lCount; i++)
  11. {
  12. if(name == songs[i].title)
  13. {
  14.  
  15. songs[i].title = songs[i+1].title;
  16. songs[i].artist = songs[i+1].artist;
  17. songs[i].genre = songs[i+1].genre;
  18. songs[i].time = songs[i+1].time;
  19. }
  20. }
  21.  
  22. return (lCount - 1);
  23. }

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.
Reputation Points: 30
Solved Threads: 3
Junior Poster
power_computer is offline Offline
144 posts
since Feb 2009
Sep 2nd, 2009
0

Re: Removing element from array

struct can be easily copied using an assingment operator.
C++ Syntax (Toggle Plain Text)
  1. songs[0]=songs[1];
You can't remove an element from the an array. However you may assign an empty struct,
C++ Syntax (Toggle Plain Text)
  1. music empty={"","","",0};
  2. songs[i]=empty;
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Sep 2nd, 2009
1

Re: Removing element from array

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.
C++ Syntax (Toggle Plain Text)
  1. bool move = false;
  2. for(int i = 0; i < lCount; i++)
  3. {
  4. if(name == songs[i].title)
  5. {
  6. move = true; //never gets reset to false
  7. continue; //we'll update on the next loop pass
  8. }
  9.  
  10. if( move )
  11. {
  12.  
  13. songs[i-1].title = songs[i].title;
  14. songs[i-1].artist = songs[i].artist;
  15. songs[i-1].genre = songs[i].genre;
  16. songs[i-1].time = songs[i].time;
  17. }
  18. }
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 2nd, 2009
0

Re: Removing element from array

Any ideas to solve my last buggy issue where in some instances when I "remove" the first element in the array, the second element is copied into the cell block [0],[1]
Reputation Points: 30
Solved Threads: 3
Junior Poster
power_computer is offline Offline
144 posts
since Feb 2009
Sep 2nd, 2009
0

Re: Removing element from array

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.
Reputation Points: 30
Solved Threads: 3
Junior Poster
power_computer is offline Offline
144 posts
since Feb 2009
Sep 2nd, 2009
0

Re: Removing element from array

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
Look at it closely, be sure you have the 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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 2nd, 2009
0

Re: Removing element from array

It is still buggy, not removing properly when I imply your method

Here is my entire code

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. struct music
  8. {
  9. string title;
  10. string artist;
  11. string genre;
  12. int time ;
  13. };
  14.  
  15. int read(char* file, music songs[10] , int lCount = 0);
  16. void print(music songs[10], int lCount);
  17. int remove(music songs[10], int lCount);
  18. int main()
  19. {
  20. music songs[10];
  21. int j = 0, lCount = 0, choice = 0;
  22. char file[256];
  23.  
  24. lCount = read("song.dat",songs);
  25.  
  26. do
  27. {
  28. cout<<endl;
  29. cout<<"Enter choice: ";
  30. cin>>choice;
  31.  
  32. switch(choice)
  33. {
  34. case 1:
  35. cout<<"Enter new file: ";
  36. cin>>file;
  37. lCount = read(file, songs, lCount);
  38. break;
  39. case 2:
  40. print(songs, lCount);
  41. break;
  42. case 3:
  43. lCount = remove(songs, lCount);
  44. break;
  45. default:
  46. cout<<" ";
  47. break;
  48. }
  49. }while(choice != 100);
  50.  
  51.  
  52.  
  53. system("Pause");
  54. return 0;
  55. }
  56.  
  57. int read(char *file, music songs[10], int lCount)
  58. {
  59.  
  60.  
  61. ifstream fin;
  62. string string;
  63. int i = 0;
  64.  
  65. for(int j=0; j < lCount; j++)
  66. {
  67. songs[i].title.clear();
  68. songs[i].artist.clear();
  69. songs[i].genre.clear();
  70. songs[i].time = 0;
  71. }
  72.  
  73.  
  74. fin.open(file);
  75.  
  76. while(!fin.eof() && i < 10)
  77. {
  78. getline(fin,songs[i].title);
  79. getline(fin,songs[i].artist);
  80. getline(fin,songs[i].genre);
  81. getline(fin,string);
  82. songs[i].time = atoi(string.c_str());
  83. i++;
  84. }
  85.  
  86.  
  87. fin.close();
  88. return(i);
  89. }
  90.  
  91. void print(music songs[10], int lCount)
  92. {
  93.  
  94. for(int j = 0; j < lCount; j++)
  95. {
  96. cout<<songs[j].title
  97. <<songs[j].artist
  98. <<songs[j].genre
  99. <<songs[j].time<<endl;
  100.  
  101. }
  102. }
  103. int remove(music songs[10], int lCount)
  104. {
  105. string name;
  106. int i =0;
  107. bool move = false;
  108.  
  109. cout<<"Enter title: ";
  110. cin.ignore();
  111. getline(cin,name);
  112.  
  113. for(int i = 0; i < lCount; i++)
  114. {
  115. if(name == songs[i].title)
  116. {
  117. move = true;
  118. continue;
  119. }
  120. if(move)
  121. {
  122. songs[i].title = songs[i-1].title;
  123. songs[i].artist = songs[i-1].artist;
  124. songs[i].genre = songs[i-1].genre;
  125. songs[i].time = songs[i-1].time;
  126. }
  127. }
  128.  
  129.  
  130.  
  131. return (lCount);
  132. }

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
Reputation Points: 30
Solved Threads: 3
Junior Poster
power_computer is offline Offline
144 posts
since Feb 2009
Sep 2nd, 2009
0

Re: Removing element from array

You got the assignment backwards, should be
C++ Syntax (Toggle Plain Text)
  1. songs[i-1].title = songs[i].title;
  2. //etc.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: My push_back keeps going into an eternal loop
Next Thread in C++ Forum Timeline: How to update a vector? ie delete elements after accessing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC