Create Inventory Management System in C

Faraz

By Faraz -

Learn how to create an advanced Inventory Management System in C Programming with a detailed step-by-step guide, from coding to compiling and running the program.


create-inventory-management-system-in-c.webp

Introduction

Creating an Inventory Management System in C programming is a great way to understand the basics of data management, file handling, and advanced menu interface design. This project will teach you how to structure your code, use functions, and manage data efficiently. Whether you're a beginner or an experienced programmer, this guide will walk you through each step to create a functional Inventory Management System with an advanced menu interface.

Step-by-Step Guide to Creating an Inventory Management System in C

Step 1: Setting Up Your Development Environment

Before we dive into coding, ensure you have a suitable C compiler installed on your computer, such as GCC. You can use any text editor like Visual Studio Code, Code::Blocks, or even a simple editor like Notepad++.

Step 2: Plan the Inventory Management System

To create an effective Inventory Management System, you need to plan the functionalities it will offer. Here’s a basic list:

  • Add New Items: Allow users to add new inventory items.
  • Display Items: Show all inventory items.
  • Search Items: Enable searching for specific items.
  • Edit Items: Modify details of existing items.
  • Delete Items: Remove items from the inventory.
  • Exit: Close the program.

Step 3: Create the Main Menu Interface

Start by creating a main menu interface that will allow users to select different operations. Use a loop to keep the program running until the user decides to exit.

#include 
#include 

void addItem();
void displayItems();
void searchItem();
void editItem();
void deleteItem();
void exitProgram();

int main() {
    int choice;
    while(1) {
        printf("============================================\n");
        printf("  WELCOME TO INVENTORY MANAGEMENT SYSTEM\n");
        printf("============================================\n\n");
        printf("  Please choose an option below:\n");
        printf("  --------------------------------------------\n");
        printf("  1. Add New Item\n");
        printf("  2. Display Items\n");
        printf("  3. Search Item\n");
        printf("  4. Edit Item\n");
        printf("  5. Delete Item\n");
        printf("  6. Exit\n");
        printf("  --------------------------------------------\n");
        printf("  Enter your choice: ");
        scanf("%d", &choice);

        switch(choice) {
            case 1: addItem(); break;
            case 2: displayItems(); break;
            case 3: searchItem(); break;
            case 4: editItem(); break;
            case 5: deleteItem(); break;
            case 6: exitProgram(); break;
            default: printf("Invalid choice! Please try again.\n");
        }
    }
    return 0;
}

Step 4: Implement the Add Item Functionality

Now that you have a basic menu interface, it’s time to implement the functionalities. Let's start with the "Add New Item" feature.

struct Item {
    int id;
    char name[30];
    int quantity;
    float price;
};

void addItem() {
    struct Item item;
    FILE *fp;

    fp = fopen("inventory.dat", "ab");
    if(fp == NULL) {
        printf("Error opening file!\n");
        return;
    }

    printf("Enter Item ID: ");
    scanf("%d", &item.id);
    printf("Enter Item Name: ");
    scanf("%s", item.name);
    printf("Enter Quantity: ");
    scanf("%d", &item.quantity);
    printf("Enter Price: ");
    scanf("%f", &item.price);

    fwrite(&item, sizeof(struct Item), 1, fp);
    fclose(fp);

    printf("Item added successfully!\n");
}

Step 5: Implement the Display Items Functionality

Next, add the function to display all the items in your inventory.

void displayItems() {
    struct Item item;
    FILE *fp;

    fp = fopen("inventory.dat", "rb");
    if(fp == NULL) {
        printf("Error opening file!\n");
        return;
    }

    printf("\nID\tName\tQuantity\tPrice\n");
    printf("-----------------------------------\n");

    while(fread(&item, sizeof(struct Item), 1, fp)) {
        printf("%d\t%s\t%d\t\t%.2f\n", item.id, item.name, item.quantity, item.price);
    }

    fclose(fp);
}

Step 6: Implement the Search Item Functionality

This function will help users search for a specific item by its ID.

void searchItem() {
    struct Item item;
    int id, found = 0;
    FILE *fp;

    fp = fopen("inventory.dat", "rb");
    if(fp == NULL) {
        printf("Error opening file!\n");
        return;
    }

    printf("Enter Item ID to search: ");
    scanf("%d", &id);

    while(fread(&item, sizeof(struct Item), 1, fp)) {
        if(item.id == id) {
            printf("\nID: %d\nName: %s\nQuantity: %d\nPrice: %.2f\n", item.id, item.name, item.quantity, item.price);
            found = 1;
            break;
        }
    }

    if(!found) {
        printf("Item not found!\n");
    }

    fclose(fp);
}

Step 7: Implement the Edit Item Functionality

Allow users to modify the details of an existing item.

void editItem() {
    struct Item item;
    int id, found = 0;
    FILE *fp;

    fp = fopen("inventory.dat", "rb+");
    if(fp == NULL) {
        printf("Error opening file!\n");
        return;
    }

    printf("Enter Item ID to edit: ");
    scanf("%d", &id);

    while(fread(&item, sizeof(struct Item), 1, fp)) {
        if(item.id == id) {
            printf("Enter new name: ");
            scanf("%s", item.name);
            printf("Enter new quantity: ");
            scanf("%d", &item.quantity);
            printf("Enter new price: ");
            scanf("%f", &item.price);

            fseek(fp, -sizeof(struct Item), SEEK_CUR);
            fwrite(&item, sizeof(struct Item), 1, fp);
            found = 1;
            break;
        }
    }

    if(!found) {
        printf("Item not found!\n");
    } else {
        printf("Item updated successfully!\n");
    }

    fclose(fp);
}

Step 8: Implement the Delete Item Functionality

Finally, add the function to delete an item from the inventory.

void deleteItem() {
    struct Item item;
    int id, found = 0;
    FILE *fp, *temp;

    fp = fopen("inventory.dat", "rb");
    if(fp == NULL) {
        printf("Error opening file!\n");
        return;
    }

    temp = fopen("temp.dat", "wb");
    if(temp == NULL) {
        printf("Error opening file!\n");
        return;
    }

    printf("Enter Item ID to delete: ");
    scanf("%d", &id);

    while(fread(&item, sizeof(struct Item), 1, fp)) {
        if(item.id != id) {
            fwrite(&item, sizeof(struct Item), 1, temp);
        } else {
            found = 1;
        }
    }

    fclose(fp);
    fclose(temp);

    remove("inventory.dat");
    rename("temp.dat", "inventory.dat");

    if(found) {
        printf("Item deleted successfully!\n");
    } else {
        printf("Item not found!\n");
    }
}

Step 9: Implement the Exit Program Functionality

Finally, implement the exitProgram function to safely exit the program.

void exitProgram() {
    printf("Exiting program...\n");
    exit(0);
}

Compiling and Running the Program

Once you have completed the coding part, compile your program using a C compiler like GCC. Here's how you can do it:

inventory_management.c -o inventory_management

After compiling, run the program:

inventory_management

Follow the on-screen instructions to add, display, search, edit, or delete inventory items.

Conclusion

Creating an Inventory Management System in C programming is an excellent way to practice your coding skills and understand how to manage data efficiently. With a clear structure and advanced menu interface, this project can be both a learning experience and a functional tool. Follow this step-by-step guide to build your own Inventory Management System and enhance your C programming skills.

By following these steps, you should now have a fully functional Inventory Management System with an advanced menu interface. This project is not only educational but also practical, giving you hands-on experience with C programming.

That’s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post

Please allow ads on our site🥺