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;


  char sname[15];

  cout<<"Enter the name of the patient:"<<endl;/*user enters name to be searched*/

  cin>>sname;

ifstream myfile ("patient.txt", ios::in );

  if (myfile.is_open())
  {
   while (!myfile.eof())
   {


    if (sname==name){
     cout<<"This is the file you searched!"<<endl;

    myfile>>number>>name>>DOB>>sex>>residence;

    }

    else {
     cout<<name;
    }
   } 


  myfile.close();
  }
  else cout << "Unable to open file";



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

Edited 3 Years Ago by pyTony: fixed formatting

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.

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.

what does public: means or it's use in the program showed above ?
im a newbie programmer :) ..

This article has been dead for over six months. Start a new discussion instead.