Selection sort with names

Hi there!

I'm studying for my programming exam and I'm now stuck with my selection sort.

I have a menu that allows the user to add a cell phone.
ex.
Model: Nokia n900
Design(flip, slide or standard): slide
Touch (yes/no): yes
price: 500$

Now after I've added lets say 5 different phones I want them to be printed out in alphabetical order with all their info.

this is my selection sort algorithm but something is wrong with it :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void showSortedName(Cell cell[], int nrOfCells)
{
	for(int i=0;i<nrOfCells-1;i++)
	{
		int smal=i;
		for(int j=i+1;j<nrOfCells;j++)
		{
			if(cell[j].getName()<cell[smal].getName())
				smal=j;
		}
	
		string temp=cell[i].getName();
		cell[i].getName()=cell[smal].getName();
		cell[smal].getName()=temp;
		cell[i].show();
	}
	system ("pause");
}


lines 12-14. You're trying to swap the Names of the phones, but getName() returns only copies of the originals. You can't assign to a copy. You could make getName() return a string& but that would destroy the point of having a getName function at all. What is more, you need to swap the cells and not their names.
Ok, but how can I do that?
swap(cell[i], cell[smal]);
hm...thats a bit strange, our teacher have not shown us that...yet...

So I have no idea what that does, would be nice if someone could write/show me more.

thanks in advance
You could just write
1
2
3
Cell temp = cell[i];
cell[i] = cell[small];
cell[small] = temp;
Ok thanks.

But how do I fix the for loops and the if statement?

ex. if(cell[j].getName()<cell[smal].getName())
I don't see anything wrong with those. You compare the strings but swap the cells. That's what it means to sort cells alphabetically.
Topic archived. No new replies allowed.