Click here to Skip to main content
15,944,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a Customer Orders project using C language. I am having a problem in coding a function that checks if the entered ID is unique. Any suggestions pls?
Posted
Comments
PIEBALDconsult 29-Dec-12 13:03pm    
What do you have so far? Are you using a database? Which one? What type of ID? { int , guid , char , other }

Are you asking the user for an ID? That doesn't seem correct.

What kind of id? Unique in what extent: in your database (are you using any DBMS?), world-wide?
By the way, entity ID's are not entered, they are generated.

So, you should not allow users to enter an ID, you should generate before creating the entity in the datastore (whatever it is). Thus you can be sure, that they are unique, at least in your application.
 
Share this answer
 
Comments
JCooperS 31-Dec-12 3:49am    
Basically, when the user selects the option to add a customer, the system asks the user to enter Customer ID. I need a function which checks whether the entered ID already exists in the customers File or not.
Zoltán Zörgő 31-Dec-12 3:53am    
I repeat: don't do this, if the customer id is the id of the entity in your system. If it is an external id, than you can ensure it with an unique constraint in the database. But than you will have to care of the exception you will get.
From your answer to Zoltan's solution I assume that you store customer data in a file. I also assume that customer IDs originally are created by another system, perhaps even a manual or paper-based one.

If you don't generate the IDs programmatically, the only way of checking their uniqueness is comparing a new ID with all other existing IDs. There are several ways to do that in a reasonable time.

(a) If you are using a simple text or binary file, you could read the file at program start and create an ordered list or tree of the existing IDs. Then update that list or tree whenever you modify the file. Your in-memory list or tree acts like a cache of all existing IDs. That method will not only tell you whether a fiven ID already exists, but also allow you to quickly find a customer record from a given ID.

(b) If you have an indexed file system or a database system at hand, just create an index on the ID field. A simple indexed read will tell you whether a given ID already exists. In other words: Let the database or index file do the work for you.

Hope that gives you some ideas.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900