Thread: Delete Data from a struct

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    4

    Delete Data from a struct

    So, Im kinda new to C. I am making a simple I/O Program but facing a problem.
    So I have this Struct:
    Code:
    struct stock{ //Struct for storing stock data
                 char item_name[100];
                 float item_rate;
                 int item_quantity;
        
                }stock_[MAX_SIZE];
    For Example, I Input the following Data:
    1.
    item_name: shoes
    item_rate: 100
    item_quantity: 200

    2.
    item_name: shoes
    item_rate: 100
    item_quantity: 200

    Now I want to delete the second entry of data from struct ie shoes, its rate and its quantity. How do I do that? For now I am using the strcmp to compare input string with that of data saved and replace it with Null string using strcpy. But that doesnt seem feasible solution to me.
    Here is my Code:
    Code:
    char item_delete[50];
    char new_name_delete[50]={'\0'};
    int compare_delete;
    retry_search_case_3:
    printf("\n\t\tEnter item name to Delete(case sensitive): ");
    scanf("%s",item_delete);
    for(i=0;i<index_stock;i++)
    {
    compare_delete=strcmp(stock_[i].item_name,item_delete);
    if(compare_delete==0)
    {
    printf("\n\t\t%s item found. Its Entry Number in Stock is %d\n\t\tDeleted this Item from Stock\n\t\tPrinting Updated Stock...\n\t\t",item_delete, i+1);
     strcpy(stock_[i].item_name,new_name_delete);
     stock_[i].item_rate=0;
    stock_[i].item_quantity=0;
    system("pause");                
    break;
     }
                                
    else
    {
    printf("\n%s item not found! Please try again: ",item_delete);
    continue;
    }
    I know how to delete a simple element from a 1D Array, I tried to implement that here but didnt work. As that algorithm compares two input integers instead of strings and then re-sorts the array

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Maybe something like this. I couldn't test it since you didn't give me a full program. I'm assuming "index_stock" is what might better be called stock_size, the current number of active elements in the array.
    Code:
        printf("Enter item name to delete(case sensitive): ");
        scanf("%s", item_delete);
     
        bool found = false;  // you can include <stdbool.h> for bool, true, false
        for (int i = 0; i < index_stock; i++)
        {
            if (strcmp(stock[i].item_name, item_delete) == 0)
            {
                printf("%s item found. Its Entry Number in Stock is %d\n"
                       "Deleted this Item from Stock\n", item_delete, i+1);
                --index_stock;
                for ( ; i < index_stock; i++)
                    stock[i] = stock[i + 1];
                found = true;
                break;
            }
        }
     
        if (!found)
        {
            printf("%s item not found.\n", item_delete);
        }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    4

    Thank you

    Thank you John C. It worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help about delete and add in struct
    By hzkids in forum C Programming
    Replies: 1
    Last Post: 10-17-2018, 03:09 AM
  2. Delete data from struct
    By John5 in forum C Programming
    Replies: 13
    Last Post: 03-14-2016, 11:40 AM
  3. Using data struct and I got an error in struct function
    By abrofunky in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2012, 07:47 PM
  4. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  5. delete memory in a struct destructor
    By JeremyCAFE in forum C++ Programming
    Replies: 5
    Last Post: 03-12-2006, 11:34 AM

Tags for this Thread