mml0707
asked on
sorting a structure
I've created a data file using an array of structures and now I'm trying to create a sort function that will sort the structure member char payTo[15]and print out the file in alphabetical order. I'm trying to use a bubble sort but I can't get it to work. I keep getting an error stating "error C2106: '=' : left operand must be l-value". I don't understand what I'm doing wrong. I've already created a search function that works fine but my sort function has me stumped !
Can anyone look at my code and give me an idea as to what I'm doing wrong ?
Here is what i have:
struct checkBook{
int check;
char payTo[15];
double amt;
char description[15];
}cb[MAX];
main()
//body of main I opened up the file for reading
void search_payTo(void)
{
char payTo[15];
int i, found;
printf("Enter pay to order of to search for: ");
gets(payTo);
found = 0;
for(i=0; i<top; i++)
if(!strcmp(payTo, cb[i].payTo)){
display(i);
found = 1;
printf("\n");
}
if(!found) printf("Not found\n");
}
void sort_payTo(void)
{
int i, j;
char temp[80];
printf("Sorted by Pay to the order of\n");
for(i=1; i<top; i++){
for(j=0; j<top-1; j++){
if(strcmp(cb[j].payTo, cb[j+1].payTo)>0)
{
temp = cb[j].payTo;
cb[j].payTo = cb[j+1].payTo;
cb[j+1] = temp;
}
}
}
}
Can anyone look at my code and give me an idea as to what I'm doing wrong ?
Here is what i have:
struct checkBook{
int check;
char payTo[15];
double amt;
char description[15];
}cb[MAX];
main()
//body of main I opened up the file for reading
void search_payTo(void)
{
char payTo[15];
int i, found;
printf("Enter pay to order of to search for: ");
gets(payTo);
found = 0;
for(i=0; i<top; i++)
if(!strcmp(payTo, cb[i].payTo)){
display(i);
found = 1;
printf("\n");
}
if(!found) printf("Not found\n");
}
void sort_payTo(void)
{
int i, j;
char temp[80];
printf("Sorted by Pay to the order of\n");
for(i=1; i<top; i++){
for(j=0; j<top-1; j++){
if(strcmp(cb[j].payTo, cb[j+1].payTo)>0)
{
temp = cb[j].payTo;
cb[j].payTo = cb[j+1].payTo;
cb[j+1] = temp;
}
}
}
}
Could you post the complete code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
first of all .. how come u have functions defined in thw body of the main
declare a function like this
int compare(void *v1,void *v2){
return strcmp((struct checkBook*)v1->payTo,(stru ct checkBook*)v2->payTo);
}
and when u have ur members filled in the
cb array
call this in the main
qsort(cb,numOfRecords,size of(struct checkBook),compare);
declare a function like this
int compare(void *v1,void *v2){
return strcmp((struct checkBook*)v1->payTo,(stru
}
and when u have ur members filled in the
cb array
call this in the main
qsort(cb,numOfRecords,size
>>>first of all .. how come u have functions defined in thw body of the main
sorry for this .. i meant how r u using the main body . where is it ?
sorry for this .. i meant how r u using the main body . where is it ?
I would suggest using a dynamic array of pointers to these structures. That way you'd only have to move around the pointers when sorting. Then there is a very useful function called qsort. Look it up and use it.
Yo! Fellow commenteers... The question is about a compile time error. It is not about how best to sort an array of structs.
Yeah, but you answered those problems already... I don't expect any credits for my comments here.
u cannot reassign array because array is nothing but a constant pointer..
and you cannot change the constant stuff once u initialise it.
and you cannot change the constant stuff once u initialise it.
No comment has been added lately and this question is therefore classified abandoned.
If asker wishes to close the question, then refer to
https://www.experts-exchange.com/help/closing.jsp
Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to ewest
Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Sunny
EE Cleanup Volunteer
If asker wishes to close the question, then refer to
https://www.experts-exchange.com/help/closing.jsp
Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to ewest
Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Sunny
EE Cleanup Volunteer