0

I want to generate a unique id for my patient class, so I referred question

Auto generate ID in C++

And the class below

class patient {

    int id;

    static int current_id;

    patient() : id(current_id++) {}
};

int patient::current_id;

For each time executing program it is generating 'current_id' from 1 to 'n'. Here n=1,2,3,4.....

I want to generate 'current_id' uniquely, even I execute program for any number of time it should be unique.

And

I am doing a c++ project to store Customer and Product information, and I want to store it in a file.

class Customer
{
    int id;
    string name;
    string type;
    unsigned long long int PHNO;
    string street,city;
    unsigned int PIN;
public:
    Customer(){}
    friend void readdata();
};
class Product
{
    int pid;
    string pname,ptype;
    unsigned int price;
    string brand_name,suitable_for;
public:
    Product(){}
    friend void readdata();
};

Here I will not take 'id' or 'pid' as input but I want to generate it automatically and then it should be stored in a file.

I also referred

How can I create a guid in MFC

but I don't know how to use in above class.

Is there any way to generate unique 'current_id' ?

If anybody know answer post suitable OR modified class also.

Community
  • 1
  • 1
IamVISH
  • 124
  • 13
  • You can store it in a file. – NathanOliver Apr 26 '16 at 18:43
  • A GUID is a guaranteed unique number, but it won't fit in an `int`. If you really do need an `int`, you need to describe your requirements a bit more. – Mark Ransom Apr 26 '16 at 19:08
  • P.S. if you *do* need a unique int, the usual way is to keep it in a database; that guarantees that multiple instances of the program can manipulate and get the value atomically. That's not an easy problem if you're doing it from scratch. – Mark Ransom Apr 26 '16 at 19:15
  • Here I want to store Customer and Product information into a file. Reading from user but I don't want to take 'id' or 'pid' as input from user. It should generate automatically each time, even I run code for any time. Normally if I use static then, if I run code first time and enter 10 customer and 15 product details then I will get 'id' from 1 to 10 and 'pid' from 1 to 15. If I run the code again then also I will get 'id' and 'pid' from 1 to n. n=1,2,3,...... Then it will become redundant 'id' and 'pid'. (Here I want to append the Customer and Product Details into file.) – IamVISH Apr 26 '16 at 19:17

0 Answers0