How to generate an auto ID (or Auto Number)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 36

Thread: How to generate an auto ID (or Auto Number)

  1. #1
    Join Date
    Feb 2012
    Posts
    17

    Auto-increment number

    Hello,

    This code is for (hospital management system).

    This code allow you to add patients.

    There are two types of patients. which is:
    1. Normal patient.
    2. Critically ill patient.

    once you added any patient. You will be able to show all patient using "ShowAllPatient;" method. -> i want the Auto-increment ID Number to appear for each patient has been added.

    And you will be able to call a patient to provide a hospital services for him/her using "GetNextPatient;" method.


    I need to generate an Auto-increment Number for each (patient) has been added. (Auto-increment Number) but it just should be different for each patient. For example the first patient will have 1, The second patient will have 2. etc. The Auto-increment number should appear for each patient.

    i really tired a lot but i could not find a solution.
    Last edited by general07z; February 19th, 2012 at 12:38 AM.

  2. #2
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,019

    Re: How to generate an auto ID (or Auto Number)

    [ Moved thread ]
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,273

    Re: How to generate an auto ID (or Auto Number)

    Quote Originally Posted by general07z View Post
    Hello,

    This code is for (hospital management system).

    This code allow you to add patients.
    Please, edit your post adding Code tags around your code snippets.
    Otherwise your code is absolutely unreadable!
    Victor Nijegorodov

  4. #4
    Join Date
    Feb 2012
    Posts
    17

    Re: How to generate an auto ID (or Auto Number)

    Quote Originally Posted by VictorN View Post
    Please, edit your post adding Code tags around your code snippets.
    Otherwise your code is absolutely unreadable!


    Thanks, but i guess there are a lot of code tags around my code.
    Not for each line, but almost for each line. enough to be understood.

    Thanks again.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,273

    Re: How to generate an auto ID (or Auto Number)

    Quote Originally Posted by general07z View Post
    Thanks, but [хл]i guess there are a lot of code tags around my code[/хл].
    Not for each line, but almost for each line. enough to be understood.
    Your "guess" is wrong! And I strongly recommend you first read the Announcement: Before you post.... and only then you may or may not "guess".

    But if you don't care where someone will read your unformatted and absolutely unreadable code snippet then you can ignore my post.
    Victor Nijegorodov

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,281

    Re: How to generate an auto ID (or Auto Number)

    We aren't talking about //comments, but phpBB [CODE][/CODE] tags

    You put them around your code, and it looks readable:

    This:
    [CODE]#include <iostream>

    int main()
    {
    std::cout << "Hello, world!\n";
    }[/CODE]

    Becomes:
    Code:
    #include <iostream>
     
    int main()
    {
       std::cout << "Hello, world!\n";
    }
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    Feb 2012
    Posts
    17

    Re: How to generate an auto ID (or Auto Number)

    Alright, i have added [code][/ code] tags around my code.

    hope my code is readable now.
    Last edited by general07z; February 16th, 2012 at 08:47 AM.

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,281

    Re: How to generate an auto ID (or Auto Number)

    There are generally two ways two ways to generate unique IDs:

    1. Incremental generation: You have a static service you can query for an ID. This service will incrementally generate numbers, and will guarantee each entry is unique.
    2. Unicity through scarcity: This is the UUID approach: The basic idea is that if you generate random 128 bit numbers, they id will be statistically unique.


    On a side note, you may want to consider using actual C++ containers, instead of rolling out your own. You want to look at:
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  9. #9
    Join Date
    Feb 2012
    Posts
    17

    Re: How to generate an auto ID (or Auto Number)

    Quote Originally Posted by monarch_dodra View Post
    There are generally two ways two ways to generate unique IDs:

    1. Incremental generation: You have a static service you can query for an ID. This service will incrementally generate numbers, and will guarantee each entry is unique.
    2. Unicity through scarcity: This is the UUID approach: The basic idea is that if you generate random 128 bit numbers, they id will be statistically unique.


    On a side note, you may want to consider using actual C++ containers, instead of rolling out your own. You want to look at:

    hey,

    Thanks for your reply.

    Now the problem is not of generating an ID, the problem is how can i specific the ID that has been generated for a patient, i mean how can i appear the number more than once for the same patient? and sometimes i will be having more than a patient. because the auto ID should be saved in the queue. so whenever i want to view all the patient, the same ID will appear for the right patient.

    i got a lot of codes for generating an Number, but the problem is how to appear the same number that has been generated for the patient when i want call him/her.



    for example this code:

    unique ID for objects

    Code:
    class IDGenerator {
       public:
          static IDGenerator * instance ();
          uint32_t next () { return _id++; }
       private:
          IDGenerator () : _id(0) {}
    
          static IDGenerator * only_copy;
          uint32_t _id;
    }
    
    IDGenerator *
    IDGenerator::instance () {
       if (!only_copy) {
          only_copy = new IDGenerator();
       }
       return only_copy;
    }


    and i will be able to get a unique ID at any time by doing:
    Code:
    IDGenerator::instance()->next ()

    But i don't know how to apply it on my code.
    any help? how to apply it on my code.

    regards
    Last edited by general07z; February 16th, 2012 at 09:27 AM.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,273

    Re: How to generate an auto ID (or Auto Number)

    Did you consider using a simple free database (like MS Access) to save your data? DB generates unique ID for you and assigns it to every new record in a table...
    Victor Nijegorodov

  11. #11
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,699

    Re: How to generate an auto ID (or Auto Number)

    Isn't this a little simpler? No need to 'new' anything.

    Code:
    uint32_t NextID()
    {
        static  uint32_t _id = 0;
    
        return _id++;
    }
    
    int main()
    {
        uint32_t id = NextID();
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  12. #12
    Join Date
    Feb 2012
    Posts
    17

    Re: How to generate an auto ID (or Auto Number)

    Quote Originally Posted by VictorN View Post
    Did you consider using a simple free database (like MS Access) to save your data? DB generates unique ID for you and assigns it to every new record in a table...

    No, im not using any database. just black screen

    Thanks for your reply.




    Quote Originally Posted by JohnW@Wessex View Post
    Isn't this a little simpler? No need to 'new' anything.

    Code:
    uint32_t NextID()
    {
        static  uint32_t _id = 0;
    
        return _id++;
    }
    
    int main()
    {
        uint32_t id = NextID();
    }


    Yes. looks simpler. but how to apply it on my code?


    looking forward for your assistance,
    Thanks a lot

  13. #13
    Join Date
    Feb 2012
    Posts
    17

    Re: How to generate an auto ID (or Auto Number)

    Quote Originally Posted by VictorN View Post
    Did you consider using a simple free database (like MS Access) to save your data? DB generates unique ID for you and assigns it to every new record in a table...
    The problem if i want to use a database, i have to change a lot of things and almost start over with my code.
    But if there is a way to use the database to save only the Auto Numbers. That is will be great.

    As the code is saving the new records in queue. but i'm able to view the records at any time. but the records will be removed once the program is closed or restart.
    So no need to save the other records in the database, IF there is a way to save only the Auto ID in the database -> But i know that it will be heavy work and header to me to do it.



    i really appreciate
    Best regards
    Last edited by general07z; February 16th, 2012 at 11:23 AM.

  14. #14
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,011

    Re: How to generate an auto ID (or Auto Number)

    Quote Originally Posted by general07z View Post
    But i don't know how to apply it on my code.
    any help? how to apply it on my code.

    regards
    In your code, each patient has a first name, last name and an ID. Shouldn't the ID be used to identify each patient then? The only problem I see, is that your ID is a string now, whereas you want it to be a number.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  15. #15
    Join Date
    Feb 2012
    Posts
    17

    Re: How to generate an auto ID (or Auto Number)

    Quote Originally Posted by D_Drmmr View Post
    In your code, each patient has a first name, last name and an ID. Shouldn't the ID be used to identify each patient then? The only problem I see, is that your ID is a string now, whereas you want it to be a number.
    hey,


    The ID in my code is defined as a "Phone Number", as showing here:

    Code:
    cout << "Phone Number : " << List[i].ID<<endl<<endl;
    You see here
    Code:
    List[i].ID
    So, We have to define another structure to the code for the auto ID, . For example "generated_ID;".

    OR, since the "ID;" function is for the "Phone number", is it possible to change it for generating the Auto ID? it should be easier for us if we did that right ?



    Regards

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center