Give an ID

This is a discussion on Give an ID within the C Programming forums, part of the General Programming Boards category; Hi , I'm new to the forum, and I hope that the people here are nice and ready to help ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    Give an ID

    Hi , I'm new to the forum, and I hope that the people here are nice and ready to help a noob like me!

    Well I'm making a program, and it is suposed to be a dyamic list of "Workers" that are composed by an:

    - Name
    - Contribute code
    - ID

    The program asks to put the name and code, and then he atributes an automatic ID.

    I don't know or have any idea to do that.

    Thanks and an Happy new Year! =)
    Last edited by MarGera; 12-30-2007 at 01:25 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Well I'm making a program, and it is suposed to be a dyamic list of "Workers" that are composed by an:

    - Name
    - Contribute code
    - ID
    the word 'dynamic' tells you that youll need a pointer for your array ('list') of workers, that can change size (ie with malloc). the 'composed' term tells you that you need to create a struct called Worker, with 3 'fields'. see the FAQ on this site for how to define and use structs, and also how to use malloc if you dont know how.

    to assign the ID, you can have a variable that starts off at the lowest value, say integer 1. when you create the first worker, assign that workers id to the 'global' id that keeps track. you then increment the id counter, then assign it again to the next worker that is created.

    please give specific questions to any of the above if you have trouble (after reading the faq)

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Quote Originally Posted by nadroj View Post
    the word 'dynamic' tells you that youll need a pointer for your array ('list') of workers, that can change size (ie with malloc). the 'composed' term tells you that you need to create a struct called Worker, with 3 'fields'. see the FAQ on this site for how to define and use structs, and also how to use malloc if you dont know how.

    to assign the ID, you can have a variable that starts off at the lowest value, say integer 1. when you create the first worker, assign that workers id to the 'global' id that keeps track. you then increment the id counter, then assign it again to the next worker that is created.

    please give specific questions to any of the above if you have trouble (after reading the faq)
    I know understand structs and malloc, I have wrote the code, but I still don't understand how to give an automatic ID, can you explain it better please?

    Thanks in advance.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I know understand structs and malloc, I have wrote the code, but I still don't understand how to give an automatic ID, can you explain it better please?
    nadroj explained it very well it you; read it again, closely.
    to assign the ID, you can have a variable that starts off at the lowest value, say integer 1. when you create the first worker, assign that workers id to the 'global' id that keeps track. you then increment the id counter, then assign it again to the next worker that is created.
    I'll take a shot at it too:

    When you're assigning IDs, you want to make sure that no two IDs are the same, right? Well, one of the easiest ways to do that is to make the first ID 0, the second 1, third 2, and so on. (Or the first 1000, etc -- whatever you like.)

    This can be done very simply by initializing a variable to the first ID you're looking for; and then every time you need an ID, look at the variable and then increment it for the next ID.
    Code:
    int id = 1000;
    
    add_worker(name, id, ...);
    id ++;  /* get next id */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    But I need it to put in a xml file (must read and interpretate it), so I don't get it. Because everytime I restart the program the ID is gonna be 0, so some workers are gonna be with the same ID. Another question, After I get the data from the worker, I need to put it in a xml file, and it is really confusing me.

    Thanks in advance.

  6. #6
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Well, one easy way to resolve this would be to save additional information in another file. Let's say we define two struct: one being the type you want the ID to be, and the other being a a "IDGenerator" type.

    Code:
    typedef unsigned int TypeID;
    
    typedef struct
    {
        TypeID nextID;
    } IDGenerator;
    You could think implementing functionality for...
    • Returning a unique ID
    • Reseting the ID generator to 0
    • Loading ID information from a FILE
    • Saving ID information to a FILE


    Here's what the function prototype could looks like (note that there is different way to do so):
    Code:
    /* Return a unique ID */
    IDGenerator giveUniqueID(IDGenerator i, TypeID *newID);
    
    /* Reset IDGenerator */
    IDGenerator resetIDGenerator();
    
    /* Load IDGenerator information from a file */
    IDGenerator loadIDGenerator(FILE *file);
    
    /* Save IDGenerator information to a file */
    void saveIDGenerator(IDGenerator i, FILE *file);
    This is just an idea on how it could be done. This would be rather straighforward to implement. And won't be complicated to use. If you have additional question, ask.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Give me some opinions on setting up a server
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-19-2004, 11:38 AM
  2. Can you give me your tip plz :)
    By dionys in forum C Programming
    Replies: 6
    Last Post: 04-12-2004, 12:14 AM
  3. Can you give me an example of a new line character?
    By Golffor1 in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2003, 12:59 PM
  4. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 02:22 PM
  5. Just to give you an idea of what we're going through...
    By rick barclay in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-13-2001, 03:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21