void doc::add_title()
{
doc d;
fstream f;
int med_lic,flag=0;
char new_title[20];
cout<<"\n\t\t\tADD NEW DOCTOR TITLE\n";
cout<<"\nEnter the Doctor's medical license number :\t";
cin>>med_lic;
f.open("doctors.da t", ios::binary|ios::in|ios::out);
while(f.read((char*)&d,sizeof(doc))&&flag==0)
{
if(d.ret_med_no()==med_lic) //d is an object of class doc
{
cout<<"\nEnter the new title:\t";//ret_med_no() is an accessor function used to return a value used to find a record.
gets(new_title);
strcat(title,new_title);//title is a private member of class doc.new title is to be added next to this one.
int pos=(-1)*sizeof(doc);
f.seekp(pos,ios::cur);
f.write((char*)&d,sizeof(doc));
flag=1;
}
else
f.write((char*)&d,sizeof(doc));
f.close();
if(flag==0)
cout<<"\nRecord not found.\n";
}
}
This function is a part of the program that accepts records (here, doctor details) from the user.One of the details enetered is Doctor Title. The program must provide the user with an option to add a new title apart from the existing title.For eg, If the "title" has "neurologist" stored in it, after adding the new title, it must be something like "neurologist, cardiologist".Please note that, this is not simple modification.The data (here, neurologist) must be stored and must not be accepted from user again.Only the new title is to be accepted and the title should be updated.
doctors.dat is a binary file and also contains other details like Name, Medical License number, contact number etc. Logically, I feel that this code should work, but I'm not able to find out whats wrong with it. The newly entered title doesn't seem to get updated. We used this part of code for simply modifying the details (accepting new details and not appending.) And this part works perfectly. However when the same logic with a bit of twist wouldn't work in the previous code.
void modify_d()
{
doc temp;
fstream f;
int s_id,flag=0;
cout<<"\nEnter the Doctor's scan center id\n";
cin>>s_id;
f.open("doctors.dat",ios::binary|ios::in|ios::out);
while(f.read((char*)&temp,sizeof(doc))&&flag==0)
{
if(temp.ret_scan_id()==s_id)
{
temp.get_mod();
int pos=(-1)*sizeof(doc);
f.seekp(pos,ios::cur);
f.write((char*)&temp,sizeof(doc));
cout<<"\n\n\tDetails updated\n";
flag=1;
}
}
f.close();
if(flag==0)
cout<<"\nRecord not found\n";
}
Can someone tell me what's wrong with the code ? Or is there any other way I can do the adding of new title ? Please make the code as closer to this version as possible as we are beginners in c++ and I'm not sure of the coding used. :(
Thanks in advance !