954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Search & Remove items from an array (struct version)

Hey guys. I'm trying to create a program which adds, searches & removes items from an array & i've been messing around with the code, trying to find answers on the internet but so far no dice. Could any of you help out?

void removeString(List& s, string strKey, const int iARRAYSIZE, int result)
{
	cout << "Enter the string to be deleted" << endl << ":" ;
	cin >> strKey;
	cout << endl ;
	for (result = 0; result < iARRAYSIZE; result++ )
	{
		if ( s.strList[result] == strKey )
		{
			for (int i = s.iListIndex; i < s.iListLength ; i++)
			{
				s.strList[s.iListIndex] = s.strList[s.iListIndex+1];
				s.strList[s.iListLength-1] = 0; //more than one operator 
			}                                       //matches these operands
			cout << "Succesfully Deleted" << endl;
		}
		else
		{
			cout << "Error 404" << endl;
		}
	}
}


This is what i have so far. I know it's crap but that's why i need help! Thanks :D

rhn94
Newbie Poster
11 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Just some few comments/questions first.

1) you might want to think twice on that hungarian notation.
2) Does the order of the elements in the string matter? For example does the last element has to be the last ? So does the element position matters relative to its neighbors?

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

1) The names are temporary, ill work on those later. What's wrong with them anyway? :S
2) not really, they are bubble sorted soon after they're added. Yes the array size is a constant so the user cannot add any more strings after the array is full and needs to delete to add anymore(obv) ..

Any hints?

rhn94
Newbie Poster
11 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

If the order doesn't matter then what you can do is
1) find the element A
2) If A does not exist then exit
3) Else if A does exist, then swap A with the last element in the array
4) Then simply set the last element to 0, and decrease the list's size

Then your done.

For me that notation is just a little annoying and sometimes can be in the way rather than helping me.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

that did not really help me but what ever .. i figured it out .. i came up with this code to anyone reading who wants this

int linearSearch(List& aList,const string& strKey) 
{
	int result;
	if(aList.iListIndex <= 0)
		result = iERROREMPTY;
	else
	{
		int i;
		for (i = 0; i < aList.iListIndex && aList.strList[i] == strKey; i++){;}
		if(i == aList.iListIndex)
			result = iERRORNOTFOUND;
		else
			result = i;

	}
	return result;
}
rhn94
Newbie Poster
11 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: