We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,985 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

2
Contributors
4
Replies
2 Weeks
Discussion Span
1 Year Ago
Last Updated
5
Views
Question
Answered
rhn94
Newbie Poster
11 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
Industrious Poster
4,044 posts since Dec 2008
Reputation Points: 851
Solved Threads: 625
Skill Endorsements: 15

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
Skill Endorsements: 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
Industrious Poster
4,044 posts since Dec 2008
Reputation Points: 851
Solved Threads: 625
Skill Endorsements: 15

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
Skill Endorsements: 0
Question Answered as of 1 Year Ago by firstPerson

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0885 seconds using 2.66MB