954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Searching for data from sequential files..

I am creating a program that stores information of hospital patients in a sequential text file..
I want to the program to be able to search for a patient using the patient name already in the text file..
The program is giving me no errors whatsoever so please look into it..

#include <iostream.h>
 
#include <fstream.h>
 
#include <string.h>
 
 
 
class Patient
 
{
 
public:
 
 char name[15];//name of the patient
 
 char DOB[10];//date of birth of the patient
 
 char sex[6];//the gender of the patient
 
 char residence[10];//the residence of the patient
 
public:
 
  int record(int number)
  {
   return number;
  }
 
  void setName(char *patName)
  {
   strcpy(name,patName);
  }
 
  void setDOB(char *patDOB)
  {
   strcpy(DOB,patDOB);
  }
  void setGender(char *patGender)
  {
   strcpy(sex,patGender);
  }
  void setResidence(char *patRes)
  {
   strcpy(residence,patRes);
  }
  double fee(double cash)
  {
   return cash;
  }
 };
 
 
 int main()
 {
  int number;
 
  char name[15];
 
  char DOB[10];
 
  char sex[6];
 
  char residence[10];
 
  double cash;
 
 
 
  <blockquote>The problem is in the code wrapped by the inline code tags</blockquote> <code> 
  char sname[15];
 
  cout&lt;&lt;&quot;Enter the name of the patient:&quot;&lt;&lt;endl;/*user enters name to be searched*/
 
  cin&gt;&gt;sname;
 
ifstream myfile (&quot;patient.txt&quot;, ios::in );
 
  if (myfile.is_open())
  {
   while (!myfile.eof())
   {
 
 
    if (sname==name){
     cout&lt;&lt;&quot;This is the file you searched!&quot;&lt;&lt;endl;
 
    myfile&gt;&gt;number&gt;&gt;name&gt;&gt;DOB&gt;&gt;sex&gt;&gt;residence;
 
    }
 
    else {
     cout&lt;&lt;name;
    }
   } 
 
 
 
  myfile.close();
  }
  else cout &lt;&lt; &quot;Unable to open file&quot;;</code> 
 
 
 
 
  cout<<"Enter patient Number: "<<endl;
 
  cin>>number;
 
  cout<<"Patient name:"<<endl;
 
  cin>>name;
 
  cout<<"DOB"<<endl;
 
  cin>>DOB;
 
  cout<<"Gender:"<<endl;
 
  cin>>sex;
 
  cout<<"residence:"<<endl;
 
  cin>>residence;
 
  cout<<"fee"<<endl;
 
  cin>>cash;
 
  Patient myPatient;
 
  myPatient.record(number);
 
  myPatient.setName(name);
 
  myPatient.setDOB(DOB);
 
  myPatient.setGender(sex);
 
  myPatient.setResidence(residence);
 
  myPatient.fee(cash);
 
  cout<<"Patient Number: "<<myPatient.record(number)<<endl;
 
  cout<<"Patient Name: "<<myPatient.name<<endl;
 
  cout<<"Patient DOB: "<<myPatient.DOB<<endl;
 
  cout<<"Patient Gender: "<<myPatient.sex<<endl;
 
  cout<<"Patient Residence: "<<myPatient.residence<<endl;
 
  cout<<"Consultation fee: "<<myPatient.fee(cash)<<endl;
 
  ofstream mfile ("patient.txt", ios::out | ios::app );
 
  if (mfile.is_open())
 
  {
  mfile << 
number<<endl<<name<<endl<<DOB<<endl<<sex<<endl<<residence<<endl
   <<cash<<endl;
 
  mfile.close();
 
  }
 
  else cout << "Unable to open file";
 
  return 0;
 }
hui
Newbie Poster
6 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

the std::string class, has it's own search function.

And if you're checking for exact matches you can do;

If string1 == string 2 Then
  do stuff


Try using it.

Also don't use EOF to read in files.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

The extraction operator >> does not read past white space (spaces and tabs). So if the name in the file contains a space your program can not work.

you should replace " while (!myfile.eof())" with this:

while( myfile>>number>>name>>DOB>>sex>>residence )
{

}

Reason: eof() does not detect end-of-file until an attempt has been made to read something. That causes the loop to run one too many times. The ifstream object will return 0 (NULL) when it reaches eof, so the loop above will not execute the loop when eof is reached.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You