Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Removing a class from an array(C++)
New Posts  All Forums:Forum Nav:

Removing a class from an array(C++)

post #1 of 10
Thread Starter 
I have this program here that I have been working on for a class, and I have spent a good while now trying to remove a class from an array.

The array is a static array of 20, and currently i'm only using the first 5 locations. If I need to remove the class at the second position of the array, the later positions (3-5), need to become 2-4. So far what I have tried has not worked.

Im not going to put much code here as I dont really want to have somebody do this for me, I just want to know how to do it, and if I am on the right track
Code:
void Team::remove()
{
   int index;
   cout << "Enter the location of the player you want to remove(0-19) \
";
   cin >> index;
   if (index < 0 || index > 19)
   {
      cout << "Invalid location";
      return;
   }
   else
   {
      getNumberUsed();

      for (int j = index; j <= numberUsed; j++)
      {
         playerList[j] = playerList[j+1];
      }
   }

}
T420
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5-2410m Lenovo Intel HD 3000 8GB GSkill DDR3 
Hard DriveOSOSMonitor
Kingston HyperX Windows 7 Ubuntu 14" 1600x900 
Case
Thinkpad 
  hide details  
Reply
T420
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5-2410m Lenovo Intel HD 3000 8GB GSkill DDR3 
Hard DriveOSOSMonitor
Kingston HyperX Windows 7 Ubuntu 14" 1600x900 
Case
Thinkpad 
  hide details  
Reply
post #2 of 10
Let's say that getNumberUsed() returns the size of the array, so if you are using the first five locations, it returns 5.

The problem is if this is how getNumberUsed() works, then you will segfault. By the way, instead of calling getNumberUsed() before the forloop, you can actually call it within the for loop in the place of numberused(which as far as I can tell, isn't even being initialized to anything).

So, back to the for loop, assuming getNumberUsed()/numberUsed is the size of the array, just think about what will happen when the array is completely full. What will happen when you try to look at the last index of the array? Let's say the maximum size of the array is 20, so the last index possible is playerList[19]. Which two indices will the line, playerList[j] = playerList[j+1] look at?
Edited by joemaniaci - 4/23/11 at 8:42pm
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
post #3 of 10
Thread Starter 
Quote:
Originally Posted by joemaniaci View Post
Let's say that getNumberUsed() returns the size of the array, so if you are using the first five locations, it returns 5.

The problem is if this is how getNumberUsed() works, then you will segfault.
i am cheating a bit in the getNumberUsed(), in fact I probably dont need it in this function. numberUsed is a private variable and I needed this function to access it elsewhere

my program compiles fine, and this runs, it just doesnt delete anything

edit: im not really sure what that would do, but it most likely would not do what i want it to do
Edited by col musstard - 4/23/11 at 8:47pm
T420
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5-2410m Lenovo Intel HD 3000 8GB GSkill DDR3 
Hard DriveOSOSMonitor
Kingston HyperX Windows 7 Ubuntu 14" 1600x900 
Case
Thinkpad 
  hide details  
Reply
T420
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5-2410m Lenovo Intel HD 3000 8GB GSkill DDR3 
Hard DriveOSOSMonitor
Kingston HyperX Windows 7 Ubuntu 14" 1600x900 
Case
Thinkpad 
  hide details  
Reply
post #4 of 10
Interesting, using the logic of my edited post above, it shouldn't even compile.

EDIT: NM, going out of bounds on an array will error out during run-time, not compile-time.

So is playerlist[] in the Team class, and if not, is it private? Even then, you should get an error for trying to access a private member variable of a class outside your own.
Edited by joemaniaci - 4/23/11 at 8:50pm
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
post #5 of 10
Thread Starter 
playerList is private within the Team class

edit: Is this line legal? Does it do what I think it should? I think if j = 2, it should put the data from location 3 of the array into location 2

playerList[j] = playerList[j+1];
Edited by col musstard - 4/23/11 at 9:18pm
T420
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5-2410m Lenovo Intel HD 3000 8GB GSkill DDR3 
Hard DriveOSOSMonitor
Kingston HyperX Windows 7 Ubuntu 14" 1600x900 
Case
Thinkpad 
  hide details  
Reply
T420
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5-2410m Lenovo Intel HD 3000 8GB GSkill DDR3 
Hard DriveOSOSMonitor
Kingston HyperX Windows 7 Ubuntu 14" 1600x900 
Case
Thinkpad 
  hide details  
Reply
post #6 of 10
Looks like what you have written, albeit ugly and will run into problems when you are working with a full array, should serve the purpose of what you are trying to do (for now).

Your array[j] reference should be reassigned to array[j +1] reference, with you only showing us parts of the code we can only speculate on what numberUsed variable represents during your programs runtime.

If i'm thinking correctly possibly you will end with two references to the same object in positions 4 and 5 (depending on whatever your numberUsed represents). Perhaps you are mistaking this for an item not being removed, when in reality it is removing the item specified just leaving the 5 slots filled with a duplicate reference.

Anyways i recommend you remove your "cheating" method as i think it may be causing your grief.
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #7 of 10
Thread Starter 
im not sure if it is actually cheating. I have numberUsed increment up whenever I add another player to the playerList array

Is there a better way of removing a player from the middle of the playerList array and then having the other players all shift down?
T420
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5-2410m Lenovo Intel HD 3000 8GB GSkill DDR3 
Hard DriveOSOSMonitor
Kingston HyperX Windows 7 Ubuntu 14" 1600x900 
Case
Thinkpad 
  hide details  
Reply
T420
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5-2410m Lenovo Intel HD 3000 8GB GSkill DDR3 
Hard DriveOSOSMonitor
Kingston HyperX Windows 7 Ubuntu 14" 1600x900 
Case
Thinkpad 
  hide details  
Reply
post #8 of 10
Quote:
Originally Posted by col musstard View Post
playerList is private within the Team class

edit: Is this line legal? Does it do what I think it should? I think if j = 2, it should put the data from location 3 of the array into location 2

playerList[j] = playerList[j+1];
Well think about this, let's say you are at the last index, when the size is 20, which means you would have playerList[19]. I think the way you have written it, playerList[j+1] will try to access playerList[20] which is very bad.

This is why with most for loops, any time you are incrementing up to the size of something, you pretty much always need, size of array - 1. Or in this case,

for (int j = index; j <= numberUsed - 1; j++)
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
post #9 of 10
Thread Starter 
Quote:
Originally Posted by joemaniaci View Post
Well think about this, let's say you are at the last index, when the size is 20, which means you would have playerList[19]. I think the way you have written it, playerList[j+1] will try to access playerList[20] which is very bad.

This is why with most for loops, any time you are incrementing up to the size of something, you pretty much always need, size of array - 1. Or in this case,

for (int j = index; j <= numberUsed - 1; j++)
good point on that.

my player class has string and 6 int's in it
to set one equal to the other as I am attempting to do in this program do I have to overload the '=' operator?
T420
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5-2410m Lenovo Intel HD 3000 8GB GSkill DDR3 
Hard DriveOSOSMonitor
Kingston HyperX Windows 7 Ubuntu 14" 1600x900 
Case
Thinkpad 
  hide details  
Reply
T420
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5-2410m Lenovo Intel HD 3000 8GB GSkill DDR3 
Hard DriveOSOSMonitor
Kingston HyperX Windows 7 Ubuntu 14" 1600x900 
Case
Thinkpad 
  hide details  
Reply
post #10 of 10
Oohhhhhhhh yehhhhhh, that is what it should be, yeh, if you want to operate on a class, you need to overload operators.

And by the way I think your for loop should actually be.

for (int j = index; j < numberUsed - 1; j++)

since you don't even want to reach the very last index, you want to reach the second to last index, which will be overwritten by the last index.
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Removing a class from an array(C++)