this is my code .can any one please tell me why the program is not giving proper results??

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

 //2d linked list major aasg#1

//structure type
struct items
{
int id;          //item no;
char name[];
int price;
char color[];
items *previous;
items *next;
};


//functions
sort();
show_all();
show();
delet_selectd();
insert_sorted();
dispose();
edit_record();
search_record();
cycle();

//gloabal varialble...
items *header=NULL;
items *footer=NULL;
items *current=NULL;
items *temp=NULL;
char ch='y';
int item_no;


//main function
main()
{
while(ch=='y'||ch=='Y')
 {
  clrscr();
  int choice;

  printf("\n**********menu**********\n");
  printf("\nshow all records__________1\n\nshow specific record______2");
  printf("\n\ndelet selectd record______3\n\ninsert any record_________4");
  printf("\n\ndelet all records_________5\n\nedit any record___________6");
  printf("\n\nenter your choice in no.s from given menu");
  scanf("%d",&choice);

  if(choice==1)
  {
     show_all();
     cycle();
  }
  else if(choice==2)
  {
     show();
     cycle();
  }
  else if(choice==3)
  {
     delet_selectd();
     cycle();
  }
  else if(choice==4)
  {
     insert_sorted();
     cycle();
  }
  else if(choice==5)
  {
     dispose();
     cycle();
  }
  else if(choice==6)
  {
     edit_record();
     cycle();
  }
  else
  {
     printf("invalid input");
     cycle();
  }
 }
  getch();
  getch();
  return(0);
}



//fuction definition

//to show all records
show_all()
{
int cos;
printf("in which order do u want to see records\n\nascending\t1\n\ndescending\t2");
cos=scanf("%d",cos);
if(cos==1)          //print in ascending order
{
 if (header==NULL)
  {
    printf("no records found");
  }
 else
  {
   current=header;
   while(current!=NULL)
   {
    printf("item no is %d",current->id);
    printf("\n");
    printf("name is %s\n",current->name);
    printf("color is %s\n",current->color);
    printf("price is %d",current->price);
    printf("\n");
    current=current->next;
   }
  }
}

else if(cos==2)          //print in decending order
{
 if (footer==NULL)
  {
    printf("no records found");
  }
 else
  {
   current=footer;
   while(current!=NULL)
   {
    printf("item no is %d",current->id);
    printf("\n");
    printf("name is %s\n",current->name);
    printf("color is %s\n",current->color);
    printf("price is %d",current->price);
    printf("\n");
    current=current->previous;
   }
  }
}
else
{
printf("invalid input");
}
 return(0);
}



//to sort all records
sort()
{
int nodes=1;
temp=header;
 while((temp->next)!=NULL)
  {
   temp=temp->next;
   nodes++;
  }

//bubblesort;

 int outer, inner;
 current=header;
 temp=header->next;
   for (outer = nodes - 1; outer > 0; outer--) {  // counting down
      for (inner = 0; inner < outer; inner++) {        // bubbling up
	  if (current->id > temp->id) {  // if out of order...
	    current->next=temp->next;
	    temp->next=current;
	    temp->previous=current->previous;
	    current->previous=temp;
	    //int temp = a[inner];          // ...then swap
	    //a[inner] = a[inner + 1];
	    //a[inner + 1] = temp;
	 }
	current=temp;
	temp=temp->next;
      }
   }
return(0);
}



//to show specific records
show()
{
search_record();
 if(current==NULL&&item_no!=current->id){printf("no records found");return(0);}
 printf("item no is %d",current->id);
 printf("\n");
 printf("%s",current->name);
 printf("\n");
 printf("%s",current->color);
 printf("\n");
 printf("price is %d",current->price);
 printf("\n");

return(0);
}



//to delete selected records
delet_selectd()
{
search_record();
if(current==NULL&&item_no!=current->id){printf("no records found");return(0);}
temp=current->next;
temp->previous=current->previous;
temp=current->previous;
temp->next=current->next;
free(current);

return(0);
}



//to insert any record in sorted order
insert_sorted()    //is just inserting a node not sorted
{
char item[20],pri[20];
current=(items *)malloc(sizeof (items));
  printf("enter id or item no.");
  gets(item);
  gets(item);
  current->id=atoi(item);
  printf("enter name");
  gets(current->name);

  printf("enter color");
  gets(current->color);

  printf("enter price");
  gets(pri);
  current->price=atoi(pri);

current->next=NULL;
current->previous=NULL;
if(header==NULL&&footer==NULL)
{
header=current;
footer=current;
}
else
{
 temp=header;
 while((temp->next)!=NULL)
  {
   temp=temp->next;
  }
 temp->next=current;
 temp=footer;
 while((temp->previous)!=NULL)
  {
   temp=temp->previous;
  }
 current->previous=footer;
 footer=current;
//sort();
}

return(0);
}



//__to delete all records
dispose()
{
 current=header;
 while (current!=NULL)
 {
  temp=current->next;
  free(current);
  current=temp;
 }
printf("all records deleted");

header=NULL;
footer=NULL;
return(0);

}

//to edit any record which is already saved
edit_record()
{
search_record();
if(current==NULL&&item_no!=current->id){printf("no records found");return(0);}
//printf("enter id or item no.");
//scanf("%d",current->id);
 printf("unedited are:");
 puts(current->name);
 printf("\n");
 puts(current->color);
 printf("\n");
 printf("price is %d",current->price);
 printf("\n");
printf("start editing");
char item[20],pri[20];

  printf("enter id or item no.");
  gets(item);
  gets(item);
  current->id=atoi(item);
  printf("enter name");
  gets(current->name);

  printf("enter color");
  gets(current->color);

  printf("enter price");
  gets(pri);
  current->price=atoi(pri);

return(0);
}

//to search any record
search_record()
{

int item_no;
printf("enter id u want to search");
scanf("%d",item_no);

 current=header;
 while (current!=NULL)
 {
  if(item_no==current->id)
  {break;}
  current=current->next;
 }

return(0);
}

//to execute program again
cycle()
{
printf("\n\ndo you want to restart the program(y/n)");
ch=getch();
if(ch!='y'&&ch!='Y')
{
exit(1);
}
return(0);
}

Recommended Answers

All 11 Replies

That completely depends on what is proper and improper results.

Asking us to read 359 lines of inconsistently formatted unexplained code to figure out
*) what the program does
*) what the program outputs
*) figure out with no clues what the proper output should be
*) where the problem is
*) how to fix it
is a little above and beyond the call of volunteers.

After 53 posts, you should know better than this.

@WaltP: Those are very good points, and I agree.
@Yamna Midhat: OK, some things I see after some minor debugging.
Line 105: Missing ampersand. Change that to

scanf("%d",&cos);

Line 336: Missing ampersand. Change that to

scanf("%d",&item_no);

At delet_selectd(): you're not checking current->next to see if it's allocated yet. This will save you some grief.

@WaltP: Those are very good points, and I agree.
@Yamna Midhat: OK, some things I see after some minor debugging.
Line 105: Missing ampersand. Change that to

scanf("%d",&cos);

Line 336: Missing ampersand. Change that to

scanf("%d",&item_no);

At delet_selectd(): you're not checking current->next to see if it's allocated yet. This will save you some grief.

thanks everybody for your response and atlast in have corrected almost all errors but now the compiler is giving following errors:
192:misplaced break
284:misplaced break
and my code is now like:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

 //2d linked list major aasg#1

//structure type
struct items
{
int id;          //item no;
int price;
items *previous;
items *next;
};


//functions
sort();
show_all();
show();
delet_selectd();
insert_sorted();
dispose();
edit_record();
search_record();
cycle();

//gloabal varialble...
items *header=NULL;
items *footer=NULL;
items *current=NULL;
items *temp=NULL;
char ch='y';
int item_no;


//main function
main()
{
while(ch=='y'||ch=='Y')
 {
  clrscr();
  int choice;

  printf("\n**********menu**********\n");
  printf("\nshow all records__________1\n\nshow specific record______2");
  printf("\n\ndelet selectd record______3\n\ninsert any record_________4");
  printf("\n\ndelet all records_________5\n\nedit any record___________6");
  printf("\n\nenter your choice in no.s from given menu");
  scanf("%d",&choice);

  if(choice==1)
  {
     show_all();
     cycle();
  }
  else if(choice==2)
  {
     show();
     cycle();
  }
  else if(choice==3)
  {
     delet_selectd();
     cycle();
  }
  else if(choice==4)
  {
     insert_sorted();
     cycle();
  }
  else if(choice==5)
  {
     dispose();
     cycle();
  }
  else if(choice==6)
  {
     edit_record();
     cycle();
  }
  else
  {
     printf("invalid input");
     cycle();
  }
 }
  getch();
  getch();
  return(0);
}



//fuction definition

//to show all records
show_all()
{
int cos;
printf("in which order do u want to see records\n\nascending\t1\n\ndescending\t2");
cos=scanf("%d",&cos);
if(cos==1)          //print in ascending order
{
 if (header==NULL)
  {
    printf("no records found");
  }
 else
  {
   current=header;
   while(current!=NULL)
   {
    printf("item no is %d",current->id);
    printf("\n");
    printf("price is %d",current->price);
    printf("\n");
    current=current->next;
   }
  }
}

else if(cos==2)          //print in decending order
{
 if (footer==NULL)
  {
    printf("no records found");
  }
 else
  {
   current=footer;
   while(current!=NULL)
   {
    printf("item no is %d",current->id);
    printf("\n");
    printf("price is %d",current->price);
    printf("\n");
    current=current->previous;
   }
  }
}
else
{
printf("invalid input");
}
 return(0);
}



//to sort all records
sort()
{
int nodes=1;
temp=header;
 while((temp->next)!=NULL)
  {
   temp=temp->next;
   nodes++;
  }

//bubblesort;

 int outer, inner;
 current=header;
 temp=header->next;
   for (outer = nodes - 1; outer > 0; outer--) {  // counting down
      for (inner = 0; inner < outer; inner++) {        // bubbling up
	  if (current->id > temp->id) {  // if out of order...
	    current->next=temp->next;
	    temp->next=current;
	    temp->previous=current->previous;
	    current->previous=temp;
	    //int temp = a[inner];          // ...then swap
	    //a[inner] = a[inner + 1];
	    //a[inner + 1] = temp;
	 }
	current=temp;
	temp=temp->next;
      }
   }
return(0);
}



//to show specific records
show()
{
search_record();
 if(current==NULL&&item_no!=current->id){printf("no records found");break;}
 printf("item no is %d",current->id);
 printf("\n");
 printf("price is %d",current->price);
 printf("\n");

return(0);
}



//to delete selected records
delet_selectd()
{
search_record();
if(current==NULL&&item_no!=current->id){printf("no records found");return(0);}
temp=current->next;
temp->previous=current->previous;
temp=current->previous;
temp->next=current->next;
free(current);

return(0);
}



//to insert any record in sorted order
insert_sorted()    //is just inserting a node not sorted
{
char item[20],pri[20];
current=(items *)malloc(sizeof (items));
  printf("enter id or item no.");
  gets(item);
  gets(item);
  current->id=atoi(item);
  printf("enter price");
  gets(pri);
  current->price=atoi(pri);

current->next=NULL;
current->previous=NULL;
if(header==NULL&&footer==NULL)
{
header=current;
footer=current;
}
else
{
 temp=header;
 while((temp->next)!=NULL)
  {
   temp=temp->next;
  }
 temp->next=current;
 temp=footer;
 while((temp->previous)!=NULL)
  {
   temp=temp->previous;
  }
 current->previous=footer;
 footer=current;
//sort();
}

return(0);
}



//__to delete all records
dispose()
{
 current=header;
 while (current!=NULL)
 {
  temp=current->next;
  free(current);
  current=temp;
 }
printf("all records deleted");

header=NULL;
footer=NULL;
return(0);

}

//to edit any record which is already saved
edit_record()
{
search_record();
if(current==NULL&&item_no!=current->id){printf("no records found");break;}
//printf("enter id or item no.");
//scanf("%d",current->id);
 printf("unedited are:");
 printf("\n");
 printf("id is %d",current->id);
 printf("\n");
 printf("price is %d",current->price);
 printf("\n");
printf("start editing");
char item[20],pri[20];

  printf("enter id or item no.");
  gets(item);
  gets(item);
  current->id=atoi(item);
  printf("enter price");
  gets(pri);
  current->price=atoi(pri);

return(0);
}

//to search any record
search_record()
{

int item_no;
printf("enter id u want to search");
scanf("%d",&item_no);

 current=header;
 while (current!=NULL)
 {
  if(item_no==current->id)
  {break;}
  current=current->next;
 }

return(0);
}

//to execute program again
cycle()
{
printf("\n\ndo you want to restart the program(y/n)");
ch=getch();
if(ch!='y'&&ch!='Y')
{
exit(1);
}
return(0);
}

you can only use break; in a loop not just in an if statement

you can only use break; in a loop not just in an if statement

thanks .
can some one please tell me that why my sort() function is not working properly???

thanks .
can some one please tell me that why my sort() function is not working properly???

what exactly happens when you use the sort() function?

what exactly happens when you use the sort() function?

this code is for 2d linked list sort function is sortind all nodes in an order with repect to id of each node.actually it is applying bubble sort on all nodes of linked list.

One thing, take out the cos= in the show_all function.

cos=scanf("%d",cos);
//Should be
scanf("%d",cos);

One thing, take out the cos= in the show_all function.

cos=scanf("%d",cos);
//Should be
scanf("%d",cos);

please also help in sort function

please also help in sort function

I believe you may be traversing past the end of your list in your sort function. Try the following modification in the sort function:

current=temp;
       // Past end of link
	[I][B]if(temp->next == NULL)[/B]
		[B]return 0;[/B][/I]
	   temp=temp->next;

you never reset current, after the first pass of the inner loop and it is sitting at the end of the list and it is not reset for the subsequent pass

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.