Removing Name from Array

(solved)
Last edited on
Again..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 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--;
}


Active your PM in your profile...
Last edited on
(clear)
Last edited on
closed account (j3Rz8vqX)
My assumptions:

You removed a member, but there is still an empty spot there...

Your current algorithm:

Declare and initialize a new array of strings equal to the size of your current array of strings.

Copy all members over. (Yes even the one you do not want.)

If you find him, you delete him afterwards, but you still have an empty spot.

A possible solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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.

How you could do this would be to locate the object to be deleted, shift everything above it left by 1 and delete the end object.

so, 5 objects..

[0][1][2][3][4]

I want to delete 2 so, I move 3 object down to 2 (original 2 now gone), move 4 object down to 3 and then finally delete 4.

To extend on my last post, something like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#include <iostream>
#include <string>
using namespace 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..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

//
//	$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;
}
Cycle starts from pos.. test..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string *temp = new string[numMembers];
	int pos = memberPosition(name);
	for (int i = pos; i < numMembers; i++)
	{
		temp[i] = members[i];

		if (temp[i] == name)
		{
			if(pos == -1)
			{
				break;
			}
			else
			{
				temp[i] = temp[i+1];
			}
		}
	}
Thanks everyone for your help! The task has been resolved.
Topic archived. No new replies allowed.