Thread: Alphabetical string sort (got it working, but not sure why)

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    55

    Alphabetical string sort (got it working, but not sure why)

    Hey all.

    I've been given an exercise to code for my MCSE, which I've done, but the problem is, I don't quite understand why it works. I was given the following skeleton code, in which I have to add several functions to:

    Code:
    #include <iostream.h>
    #include <string.h>
    
    struct student_record {
    	char name[16];
    	char course[24];
    	char age[3];
    };
    
    struct student_record stu_list[] = {
    	"Johnson Steve", "Computer Science", "24",
    	"Richards Susan", "Systems Analysis", "28",
    	"Peterson Martin", "Systems Analysis", "32",
    	"Smith Phyllis", "Advanced C", "43",
    	"Edward Peter", "Systems Analysis", "57",
    	"Bullock James", "COBOL Programming", "23",
    	"Haynes Evelyn", "C Programming", "42",
    	"Perro Lenardo", "Visual Basic Programmer", "22",
    	"Ontand Rikard", "Software Technology", "34",
    	"Iwano Morishiro", "Pascal Programming", "24",
    	"\0", "\0", "\0",
    };
    
    void sort_records(struct student_record records[], int nrecords);
    To this code, I have to add the main() function (obviously hehe), and produce the sort_records() function. Here's what I've added so far:

    Code:
    int main(void)
    {
    	int i;
    	const int NUMRECS = (sizeof(stu_list) / sizeof(stu_list[0]));
    
    	sort_records(stu_list, NUMRECS);
    
    	for (i = 0; i < (NUMRECS - 1); i++)
    	{
    		cout << "Name: " << stu_list[i].name << endl;
    		cout << "Course: " << stu_list[i].course << endl;
    		cout << "Age: " << stu_list[i].age << endl;
    	}
    
    	return 0;
    }
    
    void sort_records(struct student_record records[], int nrecords)
    {
    	int i, j;
    	struct student_record temp;
    
    	for (i = 0; i < nrecords; i++)
    	{
    		for (j = i + 1; j < nrecords; j++)
    		{
    			if (strcmp(records[i].name, records[j].name) > 0)
    			{
    				temp = *(records + i);
    				*(records + i) = *(records + j);
    				*(records + j) = temp;
    			}
    		}
    	}
    }
    After testing this, and printing the array contents (the debug loop in the main() function), it prints the contents in alphabetical order. However, I've read that you cannot copy one structure to another, or compare them etc., so I don't understand why my swapping with temp works. I can't even debug as I keep getting errors when trying to debug with this:

    Code:
    cout << *(records + i) << endl;
    I've tried this as well:

    Code:
    cout << (*(records + i)) << endl;
    The error I get is:

    error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'struct student_record' (or there is no acceptable conversion)

    So what's going on inside that nested loop? What's the pointer notation doing?

    If someone could explain this to me, I'd be *very* grateful.

    Thanks in advance,

    John.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Well, you are right that you can't compare structures like that unless you overload the comparison operators. But in fact you aren't comparing the structures, you are comparing the name member of the the structure.

    Second, I'm not sure where you read that you can't copy one structure to another, because as far as I know you can. When you are swapping them with the pointer, its the same thing as saying:

    temp = records[i];
    records[i]= records[j];
    records[j]= temp;

    To print out the information, you need to print out each member:

    cout<<records[i].name<<" "<<records[i].course<<" "<<records[i].age<<endl;

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    Well I'll be damned. Thanks very much. I only really questioned it because I thought you couldn't copy one structure to another, like with an array.

    Hmm, wonder where I got that idea from. Anyway, thanks mate, that clears that up.

    John.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM