OK, so if (strcmp(string1, string2) == 0) or equals > 0 (first string is greater than the 2nd one, or equals < 0, (second string is greater than the first string).
Sure, but can you put that into the Sort function and make it work??![]()
![]()
Give it a try. I'll check back in a bit.
After #include
inside main(), after calculationCode:void Sorter(void);
Code:Sorter();
outside main()
error C2440: 'function' : cannot convert from 'Record' to 'char *'Code:void Sorter(void) { int i, j; Record *ptr1; Record *ptr2; Record *ptrtemp; for(i = 0; i < 10-1; i++) { for(j = i+1; j < 10; j++) { ptr1 = &records[i]; ptr2 = &records[j]; if (strcmp(ptr1->student[10], ptr2->student[10]) < 0 ) { strcpy(*ptrtemp, *ptr1); strcpy(*ptr1, *ptr2); strcpy(*ptr2, *ptrtemp); } } } }
i tried putting the
Code:void Sorter(void) { int i, j; Record *ptr1; Record *ptr2; Record *ptrtemp; for(i = 0; i < 10-1; i++) { for(j = i+1; j < 10; j++) { ptr1 = &records[i]; ptr2 = &records[j]; if (strcmp(ptr1->student[10], ptr2->student[10]) < 0 ) { strcpy(*ptrtemp, *ptr1); strcpy(*ptr1, *ptr2); strcpy(*ptr2, *ptrtemp); } } } }
inside main() after the Sorter(); but it gives tons of error
Last edited by archriku; 03-26-2009 at 09:03 AM.
Code:void Sorter(void) { int i, j; Record *ptr1; Record *ptr2; Record *ptrtemp; for(i = 0; i < 10-1; i++) { for(j = i+1; j < 10; j++) { ptr1 = &records[i]; ptr2 = &records[j]; if (strcmp(ptr1->student, ptr2->student) > 0 ) { strcpy(*ptrtemp, *ptr1); strcpy(*ptr1, *ptr2); strcpy(*ptr2, *ptrtemp); } } } }student[10] is the char in the 11th spot of the student array. You don't want that. You want just "student", which is the address of the entire char array. I fixed it, above.error C2440: 'function' : cannot convert from 'Record' to 'char *'
WAIT!! NO STRCPY() EITHER!! Let the pointers do their magic!!
What a spoilsport!!![]()
![]()
![]()
Last edited by Adak; 03-26-2009 at 09:09 AM.
i change already but i get this error cannot convert from 'Record' to 'char *'
You took out the strcpy() calls? And returned the assignment operator: =
so what i have 2 do? change it back to
vbut change the total to student?Code:oid Sorter(void) { int i, j; //these are just used for counting, nothing fancy Record *ptr1; Record *ptr2; Record *ptrtemp; for(i = 0; i < 10-1; i++) { for(j = i+1; j < 10; j++) { ptr1 = &records[i]; ptr2 = &records[j]; if(ptr1->total < ptr2->total) { *ptrtemp = *ptr1; *ptr1 = *ptr2; *ptr2 = *ptrtemp; } } } }
Yepper!
WAIT! Now you've taken out the strcmp() !!
We need strcmp() to compare what the two ptr->student things are, and tell us which is greater.
Last edited by Adak; 03-26-2009 at 09:24 AM.
no more error in the compiling but i get debug error it says the variable ptrtemp is being use without initialiased
warning C4700: uninitialized local variable 'ptrtemp' used
so how to initialized ptrtemp?
Last edited by archriku; 03-26-2009 at 09:44 AM.
I don't get any warnings, so I'm not sure.
After the strcmp() line, you should have three lines of code for the ptrs:
Maybe you have a typo or something?Code:*ptrtemp = *ptr1; *ptr1 = ptr2; *ptr2 = ptrtemp;
Make sure ptrtemp is NOT mentioned in any other place except where it's declared, and here.
We could add
where it's created.Code:ptrtemp = NULL;
Last edited by Adak; 03-26-2009 at 09:55 AM.
You're welcome.![]()