Okay, so I am EXTREMELY new to programming and my teacher is not exactly a great one, sadly.

I am attempting to sort a string alphabetically.
For example:

Prompt to enter name:
Alex
Joe
John
Bill
Cait

Then sort and print:
Alex
Bill
Cait
Joe
John

Here's what I have so far:

int _tmain(int argc, _TCHAR* argv[])
{
	int num_child; // Number of children in the class
	string name[100];


	cout << "Please tell us how many children are in your class:" << endl;
	cin >> num_child;

	for(int i = 0; i < num_child; i++)
	{
		cout << "Child #: " << i+1 << "'s name is?" << endl;
		cin >> name[i];
	}
		
	for(int j = 0; j < num_child; j++)
	{	
		strcmp(name[j].c_str(), name[j+1].c_str());
		if(name[j] > name[j+1])
		{
			name[j] = name[j+1]
		}

	}

	cout << "The students in your class are" << endl;
	
	for(int k = 0; k < num_child; k++)
	{
		cout << name[k] << endl;
	}

I've entered:
Alex
Jill
Jake

and the current outcome is:

"The students in your class are:"
Alex
Jake

... can anyone point me in the right direction?

EDIT: Yes, this is for a class, so please don't just give me the code for it. I want to understand how to actually do it.

Recommended Answers

All 5 Replies

google for sort algorithms, there are several of them but the one you posted is not one of them. The easiest to code is the bubble sort.

google for sort algorithms, there are several of them but the one you posted is not one of them. The easiest to code is the bubble sort.

I've seen those, the only problem is I don't truly understand them so I don't know what is going on. Like I said, new to C++, maybe 3 months under my belt? I can simply rip an algorithm from somewhere and add my "filler" but then I have no clue what it actually does... ya know?

I've seen those, the only problem is I don't truly understand them so I don't know what is going on. Like I said, new to C++, maybe 3 months under my belt? I can simply rip an algorithm from somewhere and add my "filler" but then I have no clue what it actually does... ya know?

Yea, I know. And knowing the Bubble Sort has nothing to do with C++. I had to learn the bubble sort years before there was even a C language.

You should study the bubble sort
1) copy working code for sorting numbers instead
2) write down 4 random numbers for an array
3) run the code by hand (not on the computer)
4) write down every change to every variable on every line.

That will go a long way toward understanding. You could also find an explanation of the bubble sort and read it. That'll help too.

commented: Was that on the Whirlwind? +4

Have you used string compare? It seems like when you do the string compare, it will return a positive value if the first comes first, and a negative value if the second comes first.

Okay, so I am EXTREMELY new to programming and my teacher is not exactly a great one, sadly.

I am attempting to sort a string alphabetically.
For example:

Prompt to enter name:
Alex
Joe
John
Bill
Cait

Then sort and print:
Alex
Bill
Cait
Joe
John

Here's what I have so far:

int _tmain(int argc, _TCHAR* argv[])
{
	int num_child; // Number of children in the class
	string name[100];


	cout << "Please tell us how many children are in your class:" << endl;
	cin >> num_child;

	for(int i = 0; i < num_child; i++)
	{
		cout << "Child #: " << i+1 << "'s name is?" << endl;
		cin >> name[i];
	}
		
	for(int j = 0; j < num_child; j++)
	{	
		strcmp(name[j].c_str(), name[j+1].c_str());
		if(name[j] > name[j+1])
		{
			name[j] = name[j+1]
		}

	}

	cout << "The students in your class are" << endl;
	
	for(int k = 0; k < num_child; k++)
	{
		cout << name[k] << endl;
	}

I've entered:
Alex
Jill
Jake

and the current outcome is:

"The students in your class are:"
Alex
Jake

... can anyone point me in the right direction?

EDIT: Yes, this is for a class, so please don't just give me the code for it. I want to understand how to actually do it.

I think Selection Sort is easier to understand than Bubble Sort because we, human, use Selection Sort when we try to sort in real life.

For example, you have array of 5 integer:

[b]6	1	4	8	2[/b]

At first, you will try to find the smallest number and put it at the first element of the array. To look for the smallest number, you need to compare each number with each other. Then, you will try to find the second smallest number and put it at the second element. Then, the process keep repeatedly find third, fourth, .... smallest number to place at the third, fourth.... element of the array.

I don't expect you to understand my explanation, so I give you link to video that introduce concept of Selection Sort.

Algorithms #3 - Selection Sort

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.