Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Why, when I sort of numbers in another program they are sorted , but when I sort of string , I have an error in line 98 .

C++
struct book {
    char name[100];
    char author[50];
    int pages;
    int price;
	int yt;
};

struct element {
    book x;
    element *next;
};

void add(book x, element **head){
    element * t = new element;
    t->x = x;
    t->next = *head;
    *head = t;
}

void addLast(book x, element ** head, element **tail) {
    element * t = new element;
    t->x = x;
    t->next = NULL;
    if (*head){
        (*tail)->next = t;
    } else {
        *head = t;
    }
    *tail = t;
}



void print(element *head) {
    cout << "------------------------" << endl;
    element * t = head;
    while (t != NULL) {
        cout << t->x.name<< " | " << t->x.author << " | "
        << t->x.pages << " | " << t->x.price <<"|"<<t->x.yt<< endl;
        t = t->next;
    }
    cout << endl << "------------------------" << endl;
}

void printN(element *head,int y) {
    cout << "------------------------" << endl;
    element * t = head;
    while (t != NULL) {
		if(t->x.yt<y)>
        cout << t->x.name<< " | " << t->x.author << " | "
        << t->x.pages << " | " << t->x.price <<"|"<<t->x.yt<< endl;
        t = t->next;
    }
    cout << endl << "------------------------" << endl;
}

void printS(element *head,char *f) {
    cout << "------------------------" << endl;
    element * t = head;
    while (t != NULL) {
		if(!strcmp(t->x.name,f))
        cout << t->x.name<< " | " << t->x.author << " | "
        << t->x.pages << " | " << t->x.price <<"|"<<t->x.yt<< endl;
        t = t->next;
    }
    cout << endl << "------------------------" << endl;
}

 

void print(book b) {
    cout << b.name<< " | " << b.author << " | "
    << b.pages << " | " << b.price <<"|"<<b.yt<< endl;
}












void bS(element**head){
	bool change;
	element *t;
	do
		change=false;
		for(t=*head;t->next;t=t->next)
			if(strcmp(t->x.name,t->next->x.name>0))
			{
				strcpy(char temp,t->x.name);
				strcpy(t->x.name,t->next->x.name);
				strcpy(t->next->x.name,temp);
				change=true;
			}
		
while(change);
}
int main() {

    element *head = NULL;
    element *tail = NULL;

    book b;
    strcpy(b.name, "C");
    strcpy(b.author, "D");
    b.pages = 100;
    b.price = 1500;
	b.yt=1980;
    addLast(b, &head, &tail);

    strcpy(b.name, "C");
    strcpy(b.author, "M");
    b.pages = 300;
    b.price = 2500;
	b.yt=1982;
    addLast(b, &head, &tail);

    strcpy(b.name, "K");
    strcpy(b.author, "H");
    b.pages = 120;
    b.price = 1000;
	b.yt=1975;
    addLast(b, &head, &tail);

    print(head);
	int y=1982;
	char f[]="D";
	
	printN(head,y);
	printS(head,f);
	bS(&head);
	 cout << "\nAfter sort :\n";
   print(head);
Posted
Updated 24-May-15 3:37am
v2
Comments
Mehdi Gholam 24-May-15 8:00am    
Which line and what is the error message?
Member 11714696 24-May-15 14:53pm    
for(t=*head;t->next;t=t->next)- error is hear
In function `void bS(element**)':
lab_99.cpp:99: error: expected `while' before '(' token
lab_99.cpp:99: error: expected `)' before ';' token
lab_99.cpp:99: warning: statement has no effect
lab_99.cpp:99: error: expected `;' before ')' token

1 solution

Use the debugger.
Put a breakpoint on "line 98" - whichever that is - and run your code. When execution reaches the breakpoint, the debugger will stop running, and will wait for you to tell it what to do.
Look at the variables, and make sure that all pointers are valid, and lead to valid places. Almost certainly, you have a "bad" or uninitialized pointer in there.

We can't do this for you - we don't have access to your data to run this exactly as you do - and it's a valuable skill. It's well worth learning: it's useful in the real world as well as in IT. And it's a lot, lot easier to learn when dealing with short, simple code like this than it is when you are dealing with real-world projects!

So give it a try. It should give you information on why it's failing - and you can use that to work out where the error in your code that causes it to fail there may be: it could be a long way from the line that causes your program to fail!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900