this program is a book library that works via software.the problem is that when i sort the books by price and print them,they never get sorted!
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
static int count;
struct book
{
int bookid;
char name[30];
char author[30];
float price;
};
struct book b[40];
void add(void);
void del(void);
void sort(void);
void price(void);
void print(void);
void main(void)
{
char choice;
while(1)
{
clrscr();
printf("Enter a choice:\n 1.Add a book.\n 2.Delete a book.\n 3.Sort books by price.\n 4.To print all books details.\n 5.To print the names of the books whose price is less than 1000.\n 6.Exit\n");
choice=getche();//doing by getch() as getche makes the program rough as it is printed
switch(choice)
{
case'1':add();break;
case'2':del();break;
case'3':sort();break;
case'4':print();break;
case'5':price();break;
case'6':exit(0);
default:printf("Enter a valid choice.");break;
}
}/*switch ends*/
}
void add(void)
{
int i;
char ch[30];
clrscr();
for(i=count;i<40;i++)
{
printf("Enter books name:\n");
gets(b[i].name);
printf("Enter author's name\n");
gets(b[i].author);
printf("Enter price:\n");
gets(ch);
b[i].price=atoi(ch);
b[i].bookid=count;
break;
} /* for ends*/
count++;
printf("Dear User,the book has succesfully been added.The book id is %d",b[i].bookid);
getch();
}
void print(void)
{
int i;
clrscr();
for(i=0;i<count;i++)
{
printf("Bookid=%d,Name=%s,Author=%s,Price=%f\n",b[i].bookid,b[i].name,b[i].author,b[i].price);
}
getch();
}
void del(void)
{
int i,j;
char ch[10];
clrscr();
printf("Enter book id:");
gets(ch); // how do i put it into the structure as i dont know that which structure it belongs to
for(i=0;i<count;i++) //searching
{
if(b[i].bookid==atoi(ch))
{
for(j=i;j<count;j++)
{
b[j]=b[j+1];
}//for j ends
} //if ends
} /* for of i ends */
count--;
// sort();
getch();
}
//void del(void)
//{
// int i;
// char ch[10];
// clrscr();
//printf("Enter book id:");
// gets(ch);
// for(i=0;i<40;i++)
// {
// b[i]=b[i+1];
//
// }
// count--;
// printf("Dear user,delete succesful");
//getch();
//}
void sort(void)
{
int in,out;
struct book temp;
for(out=0;out<count-1;out++)
{
for(in=out+1;out<count;out++)
{
if(b[out].price>b[in].price)
{
temp=b[out]
b[out]=b[in];
b[out]=temp;
}
}/*for out ends*/
}//for in ends
printf("Dear user,the books are sorted by price.\n");
getch();
}
void price(void)
{
int i;
clrscr();
for(i=0;i<count;i++)
{
if(b[i].price<1000)
{
printf("%d.%s\n",i+1,b[i].name);
}
}
getch();