am making a library program that add, delete and shows record. i am done with it. but now i have a logical problem. the id i input for a book it can be taken by other books. i need to restrict ID for one book which cannot be used by other book.

like if one book have id 0001, when another book which want to input the same id ( 0001) it should restrict it to take it.

I'll assume this is a student exercise. What you need is a "global" function that issues 'the next' id.

If your code is multithreaded, then you also need that your 'nextId()' function has a mutex or some other way to serialize access to the part that actually returns an ID.

Or you could look into using a GUID (Global Universal ID) that has extremely low probability of conflict. GUID is often a hash of something like the current time in milliseconds mixed with something unique to the CPU, mixed with a pseudo-random number from a generator with a very long cycle.

Edited 5 Years Ago by griswolf: computer - > CPU (phones can generate GUID too)

ok i donot know how to explain but my problem is like this
when i input one record it have a ID . so the id is 0001. then when i input another record if i put the same id (0001) it takes the ID. it should not take the ID the id should be locked and 2nd input should not able to take that id
hope this explains.

this is the input code

#include <stdlib.h>
#include <conio.h>
#include <ctime>

using namespace std;

//book structure
struct emp
	string eno;								
	string date,published;					
	string name,author,category,publisher;

emp e;

void addEmp(); //adds new record to data file
void disEmp(); //displays all records from file
void delEmp(); //asks empNo and removes that record from file
void updEmp(); //asks empno and lets you reenter that data
void cls(); //just clear screen

int main()
	string dummy;
	int choice = -1;

	while(choice != 0)
		cout<<"===== Main Menu ====="<<endl;
		cout<<"1 -> Add Record "<<endl;
		cout<<"2 -> Display Record"<<endl;
		cout<<"3 -> Delete Record"<<endl;
		cout<<"4 -> Update Record"<<endl;
		cout<<"0 -> Exit"<<endl;
		cout<<endl<<endl<<"Selection ";
		if(choice<0 || choice>4)
		case 0:    break;
		case 1:    addEmp();    break;
		case 2:    disEmp();    break;
		case 3:    delEmp();    break;
		case 4:    updEmp();    break;

		cout<<"\nPress any key to continue...";

void addEmp()
	fstream data;
	string dummy;
	emp e;
	int choice;

		cout<<"===== Enter New Record ====="<<endl;

		getline(cin,dummy); //just to clear input buffer

		bool doOnce = true;

		while (
			( e.eno.size()<=3 ) ||										
			( e.eno.size()>4) ||										
			( e.eno.find_first_of( "0123456789" ) == string::npos)		
				cout << "Invalid ! Please Re-Enter Eno Again !!" << endl;
			doOnce = false;
			cout << "Please Enter Book's Eno (Must be 4 digits): " ;
			getline (cin, e.eno);


		cout<<"\nBook Name: ";
		getline (cin,e.name);

		cout<<"\nBook Author: ";
		getline (cin,e.author);

		cout<<"\nPublisher: ";
		getline (cin,e.publisher);

		cout<<"\nCategory: ";
		getline (cin,e.category);

		cout<<"\nYear Published: ";
		cin >> e.published;

cout<<"1 -> Save | 2 -> Cancel"<<endl;
while(!(choice==1 || choice==2));

cout<<"\nRecord Saved "<<endl<<endl;
cout<<"\n\nRecord Cancelled"<<endl<<endl;
cout<<"Enter another record?"<<endl;
cout<<"1 -> Yes | 2 -> No"<<endl;


store the existing ids in a std::set<id_type> Reject any new ID if it is in the set. You can fill the set at the top of main() by reading the existing data. ... Or, really, any other way to check whether that ID is already in use.

This article has been dead for over six months. Start a new discussion instead.