Thread: Need help on updating/editing user input (array of structures/functions)

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    3

    Need help on updating/editing user input (array of structures/functions)

    My structure:

    Code:
    typedef struct{
        int day;
        int month;
        int year;
    }full_date;
    
    
    typedef struct{
        int prod_no;
        char prod_name[30];
        char prod_brand[30];
        int quantity;
        float price;
        full_date date_added;
    }inventory;
    What I want is for only price and quantity to be edited/updated.

    My UpdateProduct function:

    Code:
    void UpdateProduct(inventory *prod[], int size){
        
         int prod_num;
        
        
        printf("Note: Only Quantity and Price can be updated.");
        
        printf("Enter product # of specified product to update: ");
        scanf("%d",&prod_num);
        
        printf("Enter new price: ");
        scanf("%f",&prod[prod_num]->price);
        
        printf("Enter new quantity: ");
        scanf("%d",&prod[prod_num]->quantity);
        
        
        
    }
    in main() I declared the structure as "inventory prod[size]"

    The error/warning I get from the compiler is "undefined reference to UpdateProduct(inventory*, int)'"

    Am I supposed to make prod a pointer in main and on all my other functions?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    I'd start by changing this:
    Code:
    typedef struct{
        int prod_no;
        char prod_name[30];
        char prod_brand[30];
        int quantity;
        float price;
        full_date date_added;
    }inventory;
    To this:
    Code:
    typedef struct {
        int prod_no;
        char prod_name[30];
        char prod_brand[30];
        int quantity;
        float price;
        full_date date_added;
    } product;
    
    typedef struct {
        product *products;
        int size;
    } product_inventory;
    Now, your function becomes:
    Code:
    void UpdateProduct(product_inventory *inventory)
    In main, you would write:
    Code:
    product_inventory inventory;
    inventory.size = 123; // for example
    inventory.products = malloc(sizeof(inventory.products[0]) * inventory.size);
    // ...
    UpdateProduct(&inventory);
    // ...
    free(inventory.products);
    If you have not learnt about malloc and free, then it is even easier: just declare products to be an array with a fixed size, then use size to denote the number of elements in use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input problem when using user-defined functions.
    By 843 in forum C Programming
    Replies: 12
    Last Post: 10-16-2010, 12:33 PM
  2. Replies: 12
    Last Post: 03-11-2010, 02:28 PM
  3. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  4. running shell functions based on user input.
    By System_159 in forum Linux Programming
    Replies: 14
    Last Post: 10-12-2007, 07:19 PM
  5. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM

Tags for this Thread