Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on How can I delete a book from the collection by a particular title from my library file?

Post

How can I delete a book from the collection by a particular title from my library file?

+1
−3

I would like to delete a book from the collection by a particular title from my library if the user enters 2 as input for my switch statement. How can I do that with my current code?

#include<stdlib.h>
#define MAX 49
//void addBook(struct library* thislib){
//}
struct book{
     char *title;
     char *author;
     char *subject; //subject[]
};

struct library {
    struct book collection;
    int num_books;
    struct library *next; //store address of next node
};

struct book collection; //supposed to be aBook
struct book *aPtr;
struct library aCollection;
struct library *aaPtr;

int main (void){
    
   FILE *cfPtr; //pointer points to the newly created file

   char strtitle[MAX]; 
   char strauthor[MAX];
   char strsubject[MAX];
   int operation;

   
    if ((cfPtr = fopen("library.dat","w")) == NULL){
        printf("File could not be opened...\n");
    }
   else {

       while (!feof()){
        operate();
        printf("Enter an operation: ");
        scanf("%d", &operation);

          /*case 1: coding of the switch statement*/

            case 2: //here
            if(!isEmpty(aPtr)){ //if list is not empty
                aaPtr = NULL;
                print("%s", "Enter title of the book to delete:\n");
                scanf("%s", &strtitle);

                aCollection.collection = strtitle;
                delete(strtitle);
                printf("The book %s has been removed from the library.", aBook.title);
                fprintf(cfPtr, "The book %s has been removed from the library.", aBook.title);
                }
                else{
                    printf("Item not found!");
                }
                else{
                    puts("List is empty.\n");
                }
            }
            break;

/*unfinished coding, I'm still working on this code*/
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

What Canina said, but just a few more additional tips... (2 comments)
Post Feedback (1 comment)
What Canina said, but just a few more additional tips...
elgonzo‭ wrote about 3 years ago · edited about 3 years ago

Before you start coding/implementing, spend some effort in actually designing/modelling both the desired application/business logic and the related data structures in detail and completely.

From your code snippet, it rather looks like that you started coding just with a vague notion but lacking a detailed picture of "what" you actually want to realize (business logic + data structures). It seems to me you focus too early on the "how" (i.e., coding the implementation).

This resulted in your rather confusing and silly data structures, where you have modelled multiple libraries as a linked list, with each library being able to hold only a single book. And then you call the single book in a library a "collection". It's confusing/nonsensical and a bit messy, kinda like taking a quick shot from the hip without taking aim, wouldn't you agree? ;-)

elgonzo‭ wrote about 3 years ago · edited about 3 years ago

For now i personally feel it would serve you much better if you separate both concerns (modelling you application/business logic + data structures vs. the implementation of the logic + data structures), and try to do them separately and sequentially as much as possible.

In the future, if you have more experience and routine in coding, you will be able to do the modelling and implementation (at least partially) at same time... (and wing some of the stuff, like the rest of us do... ;-P )