Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 958937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:56:41+00:00 2026-05-16T00:56:41+00:00

I have made a program which is a small library operated via software. When

  • 0

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 count-- in the del() function. I cannot rely on printing the count as the bookid. Is there a better option?

#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);
    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<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--;
  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 i;
  float temp;
  for(i=0;i<40;i++)
  {
    if(b[i].price>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<count;i++)
  {
    if(b[i].price<1000)
    {
      printf("%d.%s\n",i+1,b[i].name);
    }
  }
  getch();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:56:42+00:00Added an answer on May 16, 2026 at 12:56 am

    One way is to have two 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.

    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.)

    #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 < MAXBOOKS; i++) {
        if (! b[i].valid) return i;
      }
      return -1; // no more space
    }
    
    void add(void)
    {
      int i = findEmpty();
      char ch[MAXSTR];
    
      if (i < 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();
    }
    

    del() now just marks the book as invalid. The main part of del() looks like this.

    gets(ch);
    int idToDelete = atoi(ch);
    // find which structure it belongs to
    for(i=0;i

    Use a selection sort 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 474k
  • Answers 474k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take it out of the markup then. I don't think… May 16, 2026 at 4:13 am
  • Editorial Team
    Editorial Team added an answer To resolve this issue it is important to follow certain… May 16, 2026 at 4:13 am
  • Editorial Team
    Editorial Team added an answer I needed to set the culture in web.config for example… May 16, 2026 at 4:13 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.