hello
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.

Recommended Answers

All 5 Replies

I don't understand the problem can you post the relevant source and a better explanation of the problem.

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.

Use an array that holds all the IDs in use.

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<iostream>
#include<fstream>
#include<string>
#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)
	{
		cls();
		cout<<"===== Main Menu ====="<<endl;
		cout<<endl<<endl;
		cout<<"1 -> Add Record "<<endl;
		cout<<"2 -> Display Record"<<endl;
		cout<<"3 -> Delete Record"<<endl;
		cout<<"4 -> Update Record"<<endl;
		cout<<endl;
		cout<<"0 -> Exit"<<endl;
		cout<<endl<<endl<<"Selection ";
		cin>>choice;
		if(choice<0 || choice>4)
		{
			cls();
		}
		switch(choice)
		{
		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...";
		getch();
	}
}

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

	do
	{
		cls();
		cout<<"===== Enter New Record ====="<<endl;
		cout<<endl<<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)		
			)
		{
			if(!doOnce)
				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<<endl<<endl;
cout<<"1 -> Save | 2 -> Cancel"<<endl;
cin>>choice;
}
while(!(choice==1 || choice==2));

if(choice==1)
{
data.open("data.dat",ios::out|ios::app);
data.write((char*)&e,sizeof(e));
data.close();
cout<<"\nRecord Saved "<<endl<<endl;
}
else
{
cout<<"\n\nRecord Cancelled"<<endl<<endl;
}
cout<<"Enter another record?"<<endl;
cout<<"1 -> Yes | 2 -> No"<<endl;
cin>>choice;
if(choice==1)
addEmp();
else
return;

}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.