Note that there are some explanatory texts on larger screens.

plurals
  1. POWhile performing operations will the bookid's get changed?
    primarykey
    data
    text
    <p>I have made a program which is a small library operated via software. When I add two books and then delete the first book the second book gets the same bookid as the first book because of <code>count--</code> in the <code>del()</code> function. I cannot rely on printing the count as the bookid. Is there a better option?</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;conio.h&gt; #include&lt;stdlib.h&gt; 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&lt;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); printf("Dear User,the book has succesfully been added.The book id is %d",i); count++; break; } /* for ends*/ getch(); } void print(void) { int i; clrscr(); for(i=0;i&lt;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&lt;count;i++) //searching { if(b[i].bookid==atoi(ch)) { for(j=i;j&lt;count;j++) { b[j]=b[j+1]; }//for j ends } //if ends } /* for of i ends */ count--; getch(); } //void del(void) //{ // int i; // char ch[10]; // clrscr(); //printf("Enter book id:"); // gets(ch); // for(i=0;i&lt;40;i++) // { // b[i]=b[i+1]; // // } // count--; // printf("Dear user,delete succesful"); //getch(); //} void sort(void) { int i; float temp; for(i=0;i&lt;40;i++) { if(b[i].price&gt;b[i+1].price) { temp=b[i].price; b[i].price=b[i+1].price; b[i+1].price=temp; } }/*for ends*/ printf("Dear user,the books are sorted by price.\n"); getch(); } void price(void) { int i; clrscr(); for(i=0;i&lt;count;i++) { if(b[i].price&lt;1000) { printf("%d.%s\n",i+1,b[i].name); } } getch(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload