Thread: Question on an array structure

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    2

    Question on an array structure

    I have 3 structures for which information is entered by the user and stored in a .bin file
    Code:
    struct date{    int day, month, year;
    };
    
    
    struct detail{
        float quantity;
        struct date expdate;
    };
    
    
    struct item{
        int itemID;
        char name[20], measure[10], lowestamt[10], status[10];
        struct detail q[10];
    };
    
    
    struct item Ingredient;
    In the struct "Ingredient", I have an array struct "q" which can store up to 10 elements. In the program, only one element of the array q would be filled at any one time and I'd like the user to be able to edit the item record and add information to the next element in the array q. So if q[x] is filled, I'd like to add information to q[x+1]. My question is, how do I find out what the value of x is for each item record in the file?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Two possibilities. You either need to save the current number of used elements as an extra int in item, or you could use a special value to indicate unused elements (or at least the first unused element). For example, if quantity cannot be negative, than setting it to -1 would indicate an unused element. You also need to handle the case where q (bad name) is full.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    2
    Quote Originally Posted by john.c View Post
    Two possibilities. You either need to save the current number of used elements as an extra int in item, or you could use a special value to indicate unused elements (or at least the first unused element). For example, if quantity cannot be negative, than setting it to -1 would indicate an unused element. You also need to handle the case where q (bad name) is full.
    I tried the first suggestion and the program looks to be working as I want it to now. I've also taken into consideration your reminder about q being full. (I shall be changing the name "q" by the way, I were experimenting with this part so I wanted something that was easy to type.)
    Thank you very much for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-21-2016, 07:34 PM
  2. Structure array question
    By Glassjaw in forum C Programming
    Replies: 5
    Last Post: 11-23-2015, 12:27 AM
  3. A question about structure and array
    By winggx in forum C Programming
    Replies: 2
    Last Post: 04-03-2010, 10:28 PM
  4. question about holding a structure in a char array
    By spank in forum C Programming
    Replies: 8
    Last Post: 09-04-2007, 07:21 AM
  5. Array and Structure Question
    By loopshot in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2004, 05:10 PM

Tags for this Thread