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";

     }
    }
The concept

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 !

Hello everyone, I have figured out something else for this code. Here it goes.

        void doc::get_add() //this is a member of class doc.
    {

     char new_title[20];
     cout<<"\nEnter the title\n";
     gets(new_title);
     strcat(title," ");
     strcat(title,new_title);
    }
    void add_title()//this is the funtion called in the main.
    {
     doc temp;
     fstream f;
     int s_id,flag=0;
     cout<<"\nEnter the Doctor's scan center id\n";//asking the user for searching the records
     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)//ret_scan_id() is a funtion that returns the doctor id and checks for validation
      {
       temp.get_add();
       int pos=(-1)*sizeof(doc);
       f.seekp(pos,ios::cur);
       f.write((char*)&temp,sizeof(doc));
       cout<<"\n\n\tTitle updated\n";
       flag=1;
      }
     }
     f.close();
     if(flag==0)
     cout<<"\nRecord not found\n";
    }

So, this one works :) . This is a part of our school project, so i'm unable to post the complete program. Hope this helps !

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.