void Club::removeMember(string name)
{
string *temp = new string[numMembers];
int pos = memberPosition(name);////
for (int i = pos; i < numMembers; i++)
{
if (members[i] == name)
{
if( pos == -1)
{
break; //break because nothing should be done if name is not in the array
}
else
{
members[i] = members[i+1]; ///
}
}
}
numMembers--;
}
void Club::removeMember(string name)
{
string *temp = new string[numMembers-1];//Reduce size of array by 1; kick him out for good, hopefully...
for (int i = 0; i < numMembers; i++)
{
//temp[i] = members[i]; //Do not copy yet, please.
if (members[i] == name)//Let us check the member first, it's unnecessary to check temp[i];
{
if(memberPosition(name) == -1)//Ah we found him! I think...
{
//break; //break because nothing should be done if name is not in the array
//WEll.. we won't do nothing here.. Do not add him, as the else does.
}
else
{
//Add the member, if it isn't HIM!!
temp[i] = members[i];
//does the removing
}
}
}
if ( numMembers > 0)
{
delete [] members;//Delete the old array =D
}
members = temp;//Reassign the old array pointer to temps pointer
numMembers--;//reduce logical count of array size, for ourselves to know
//Done good job.
}
int Club::memberPosition (string name) const
{
for (int i = 0; i < numMembers; i++)
{
if (members[i] == name)
{
return i;
}
}
return -1;
}
Have fun. (I may have made a mistake, it made sense when I designed it..)
All code was typed in the web browser.
#include <iostream>
#include <string>
usingnamespace std;
// function prototype
int deleteName(string *objects, int numObjects, string name);
int main()
{
string *names;
int objectsCount = 5; // number of names.
names = new string[5]; // strings 0 - 4
// lets fill with some demo data
names[0] = "Michael";
names[1] = "Paula";
names[2] = "Deborah";
names[3] = "Simon";
names[4] = "Dylan";
// display before we delete.
cout << endl << endl << "Before... " << endl << endl;
for (int i = 0; i < objectsCount; i++)
cout << names[i] << endl;
// delete a name
objectsCount = deleteName(names, objectsCount, "Deborah");
// whats it look like now?
cout << endl << endl << "After... " << endl << endl;
for (int i = 0; i < objectsCount; i++)
cout << names[i] << endl;
// free memory
delete[] names;
return 0;
}
//
// $function: deleteName()
//
int deleteName(string *objects, int numObjects, string name)
{
if (numObjects == 0)
return numObjects;
for (int i = 0; i < numObjects; i++)
{
if (*(objects+i) == name) {
// i now points to index where our
// name is so everything above it
// we need to move down 1.
for (int j = i; j < numObjects; j++)
*(objects+j) = *(objects + j + 1);
return numObjects - 1;
}
}
// if we get here the name wasnt found.
return numObjects;
}
If you prefer array notation instead of pointer notation it would look lilke..
//
// $function: deleteName()
//
int deleteName(string *objects, int numObjects, string name)
{
if (numObjects == 0)
return numObjects;
for (int i = 0; i < numObjects; i++)
{
if (objects[i] == name) {
// i now points to index where our
// name is so everything above it
// we need to move down 1.
for (int j = i; j < numObjects; j++)
objects[j] = objects[j + 1];
return numObjects - 1;
}
}
// if we get here the name wasnt found.
return numObjects;
}