Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way is to have <em>two</em> global counters: one has the number of books stored, the other gives the next (unique) book id, as Tyler McHenry points out. When you delete a book, decrement the number of books, but never decrement the next book id.</p> <p>I notice that when a book is deleted, you move the rest of the book entries together ("compress" them) so your array is always "dense". So another solution is to add a "deleted" flag to the book structure. Now you don't compress the entries when a book is deleted, but when a book is added, the code must search for an empty place in the array. Also the bookid is just the position in the array. (By the way, if you hard-code sizes, like the size of an array, do it once.)</p> <pre><code>#define MAXSTR (30) struct book { int bookid; int valid; // 1 is valid, 0 is empty or deleted char name[MAXSTR]; char author[MAXSTR]; float price; }; #define MAXBOOKS (40) struct book b[MAXBOOKS]; int findEmpty() { int i; for (i=0; i &lt; MAXBOOKS; i++) { if (! b[i].valid) return i; } return -1; // no more space } void add(void) { int i = findEmpty(); char ch[MAXSTR]; if (i &lt; 0) return; // no room for more books clrscr(); printf("Enter books name:\n"); fgets(b[i].name, MAXSTR, stdin); printf("Enter author's name\n"); fgets(b[i].author, MAXSTR, stdin); printf("Enter price:\n"); fgets(ch, MAXSTR, stdin); b[i].price=atoi(ch); /* Assign the empty location found to this book */ b[i].bookid = i; /* mark that it is valid */ b[i].valid = 1; printf("Dear User,the book has succesfully been added.The book id is %d", b[i].bookid); getch(); } </code></pre> <p>del() now just marks the book as invalid. The main part of del() looks like this.</p> <p>gets(ch); int idToDelete = atoi(ch); // find which structure it belongs to for(i=0;i <p>Use a <a href="http://www.itl.nist.gov/div897/sqg/dads/HTML/selectionSort.html" rel="nofollow noreferrer">selection sort</a> in sort() instead of the one-pass routine (which doesn't work in all cases) which is there. Printing books should skip any book that is not valid, too.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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