Click to See Complete Forum and Search --> : selection sorting


duskrue
March 28th, 2008, 06:42 PM
Hi, I'm making a program that's meant to take 20 names from a user, sort them into alphabetical order using selection sorting, and output the names in order. I decided to make the problem a little simpler by using numbers first instead. So it takes twenty numbers from the user, puts them in order, and then outputs the numbers in the correct order. (You get the idea)

However, I'm running into problems. I fixed the program in Visual Studio to the point that all my errors went away, but now I'm getting new errors saying:

"1>c:\users\spenser\documents\visual studio 2005\projects\sortnames\sortnames\sorting.cpp(54) : error C2556: 'void Swap(int [],int,int)' : overloaded function differs only by return type from 'int Swap(int [],int,int)'
1> c:\users\spenser\documents\visual studio 2005\projects\sortnames\sortnames\sorting.cpp(7) : see declaration of 'Swap'
1>c:\users\spenser\documents\visual studio 2005\projects\sortnames\sortnames\sorting.cpp(54) : error C2371: 'Swap' : redefinition; different basic types
1> c:\users\spenser\documents\visual studio 2005\projects\sortnames\sortnames\sorting.cpp(7) : see declaration of 'Swap'"

I've attached the source code for viewing. I can't see any problems, but obviously something is wrong.

Scratch that. I just noticed I had function Swap as an int, and then a void later. So yeah. . Now I just need help with my problems switching it over to names.

Paul McKenzie
March 28th, 2008, 06:47 PM
I've attached the source code for viewing. I can't see any problems, but obviously something is wrong.The error is obvious:

int Swap(int [], int, int);
//...
void Swap (int array[], int j, int k)

The Swap function is declared as returning an int, but in the implementation, it returns void.

Regards,

Paul McKenzie

duskrue
March 28th, 2008, 07:10 PM
Yeah, I saw that and edited the post.

I altered the code to take names instead, and I'm not getting any errors with that. The problem that the compiler obviously won't pick up is that when it does out put the names, they aren't in order.

The updated code is in the attachment below.

duskrue
March 28th, 2008, 10:54 PM
I tried switching to bubble sort, but that just gets me more lost having to rewrite stuff.

Anyone's input would be great :)

whisky
March 29th, 2008, 01:27 AM
Comparing integers is easy using the < , > or = operators.

You need to use a string compare (strcmp()) for comparing 2 strings (char arrays). Check the link for an example.

http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html