0

This is my code. I want to check whether or not the value the user entered for idnum already exists as the idnum for any other patient in the file "patient.dat". This code doesn't show any error but it doesn't work logically either. I'm using the TurboC 7 compiler.

void patient::idnum(patient p)
{
   do
   {
     clrscr();
     int found4=0;
     cout<<"enter a 4 digit id number";
     cin>>idno;
     ofstream f3;
     ifstream f4;

     f4.open("patient.dat",ios::in);
     f4.seekg(0);

     while(!f4.eof())
     {
       f4.read((char*)&p,sizeof(p));
       if(p.getid()==idno)
         cout<<" id number already in use.. try again";
       else
         found4=1;
     }

     if(found4==1)
     {
       cout<<"valid entry... pls continue";
       f3.open("patient.txt",ios::out|ios::app);
       f3.write((char*)&p,sizeof(p));
       f3.close();
       getch();
     }

   } while(p.getid()!=idno);
}
Sergey Kolesnik
  • 3,009
  • 1
  • 8
  • 28
  • Welcome to StackOverflow. Please format your question for language, code indentation and add the error or any other information that could be helpful to understand the issue. – Azeem Dec 14 '19 at 04:00
  • 1
    Note that using similar names `"patient.dat"` and `"patient.txt"` is indicative of probable problems. Is that meant to be the same file name, or did you really intend to use two different files? If two files, then you run into problems if the user decides to enter an `idnum` value of `9999` multiple times; you won't find it in `"patient.dat"` and will keep adding it to `"patient.txt"`. Use a variable to hold the file name if you're dealing with one file. Or, if you prefer C-like conventions, use a `#define PATIENT_FILE "patient.dat"` and reference `PATIENT_FILE` in both `open()` calls. – Jonathan Leffler Dec 14 '19 at 04:30
  • 1
    The code may compile, but there are a LOT of things wrong with it from a logic standpoint: Lack of error checking. Not opening the two files in binary mode. Reading/writing class objects without adequate serialization. Using `while(!eof())`. Not breaking the inner loop if the ID is found. Not managing the value of `found4` correctly. Using the wrong condition to continue or break the outer loop. Passing in a `patient` object as an input parameter when it is really not used as input. – Remy Lebeau Dec 14 '19 at 04:31
  • [Why !.eof() inside a loop condition is always wrong.](https://stackoverflow.com/q/5605125/9254539) Why not use something like [std::unordered_set](https://en.cppreference.com/w/cpp/container/unordered_set) – David C. Rankin Dec 14 '19 at 04:36
  • 1
    _"it doesn't work logically"_ -- I could guess as much from the fact that you're asking a question here. What does it do? What did you expect it to do? Use concrete examples of input and output, please. – JaMiT Dec 14 '19 at 04:41
  • In order to think like a programmer, you need to state specific things about what it does wrong. As it is, you're not only asking for people to solve the issue, but you're asking them to figure out what the problem is too! *"doesn't work logically"* isn't good enough. That's what a non-programmer would say. – Jonathan Wood Dec 14 '19 at 20:46

0 Answers0