Thread: Auto generated ID V2

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    61

    Auto generated ID V2

    I need help understand that code

    Auto generated ID

    Code:
    int nextid ( ) {
    static int last = 0;
    return ++last;
    }
    Code:
    struct ANIMAL {
        char especie[MAX]; // ID from specie
        int id; // ID Animal        
        char nome[MAX]; // Name of the animal
        int peso; // Weight
        char nomelocal[MAX]; // Place where the animal live
        pno prox; // Pointer for the next 
    
    
    };
    What i want to do is the following, i am loading animals from a text file each one i want to have a diferent ID.
    (Run two diferent files one after the other and got OK)


    Or in the middle of the program i want to make a baby animal and give him a automatic ID. ( Dindīt try it yet, still need to build function)

    I think that code will ok, but first i don't fully understand with.

    But after that part of ID i want it to remember the species.

    Monkey: 1
    Monkey: 2
    Lion: 1
    Zebra: 1
    Monkey: 3
    Snake: 1

    How can i approach my problem ?
    Last edited by thinkabout; 05-10-2017 at 03:13 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    With the code you posted, you would get
    Monkey: 1
    Monkey: 2
    Lion: 3
    Zebra: 4
    Monkey: 5
    Snake: 6

    The ID is globally unique for all your animal records, regardless of species.

    If you want a per-species ID, then you need to store a separate record of
    Code:
    struct idgen {
      char especie[MAX];
      int nextId;
    };
    int nextid ( char especie[MAX] ) {
      /// perform some kind of lookup
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It seems that you just want separate counts of the different species, using the current count as the id for an individual. But with sentences like "I think that code will ok, but first i don't fully understand with", it's hard to tell what you want.

  4. #4
    Registered User
    Join Date
    May 2017
    Posts
    61
    Quote Originally Posted by algorism View Post
    It seems that you just want separate counts of the different species, using the current count as the id for an individual. But with sentences like "I think that code will ok, but first i don't fully understand with", it's hard to tell what you want.
    That was because i dindīt know static int, now i do because of the forum. Thanks

    C static variable example - YouTube


    I already upgraded a little bit the generator, because if i close the program and open again the counter will return back to 0.

    Now i read the last value from one file and write after i increment it.


    Now i need the v3

    I want a alphanumeric generator. (Still thinking about the format i want, i just know that need to be alphanumeric )

    ID Specie: monkey2: SerialNumber: 1
    ID Specie: 4pig: SerialNumber: 1
    ID Specie: monkey2: SerialNumber: 2
    ID Specie: monkey2: SerialNumber: 3

    I will store the information on a linked list or variable length array.


    Sorry for my poor english.
    Last edited by thinkabout; 05-12-2017 at 07:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-24-2015, 11:39 AM
  2. Replies: 2
    Last Post: 11-20-2013, 01:34 PM
  3. Auto generated ID
    By ulti-killer in forum C Programming
    Replies: 3
    Last Post: 10-13-2012, 02:04 AM
  4. [VS 2005] Auto generated skeleton code
    By cboard_member in forum Tech Board
    Replies: 4
    Last Post: 03-06-2006, 12:45 PM
  5. Auto generated
    By Daniel decosta in forum C Programming
    Replies: 11
    Last Post: 09-28-2002, 05:35 PM

Tags for this Thread