title: stuck in search from file in c++:
body: <p>I'm new.I am making a hospital management system and i am stuck at a point where i have to search all the patients with specific doctor. I have developed a code but it does print everything present in the file. i only want to search with doctor name and every patient with that doctor should come up.
  here is my coding(the bold function is actually my problem):</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include &lt;fstream&gt;
  #include&lt;ctime&gt;
  #include&lt;conio.h&gt;
  #include&lt;string.h&gt;
  using namespace std;
  
  
  
  //FUNCTION DECLERATION
  void WelcomeScreen();//WelcomeScreen function decleration
  void Title();//Title function decleration
  void MainMenu();//MainMenu function decleration
  void LoginScreen();//LoginScreen function decleration
  void Add_patient();// add patient
  void func_list();//function to list the patient details
  void Search_rec();//Search_rec function declaration
  void search_p();   //void Edit_rec();//Edit_rec function declarationvoid
    //void Dlt_patient();//Dlt_rec function declaration
    //void ex_it(void);//exit function declaration
  
  
  
  struct patient//structure
  {
  int id;
  int age;
  char Gender;
  char First_Name[20];
  char Last_Name[20];
  char Contact_no[15];
  char Address[60];
  char Email[30];
  char Doctor[20];
  char Problem[20];
  patient* next;
  patient* prev;
  // char time [];
  
  };
  
  patient* patient_head = NULL;
  
  
  /* ************************************************* Welcome Screen ********************************************* */
  void WelcomeScreen() //function for welcome screen
  {
  
  cout &lt;&lt; "\n\n\n\n\n\n\n\t\t\t\t#########################################";
  cout &lt;&lt; "\n\t\t\t\t#\t\t WELCOME TO             #";
  cout &lt;&lt; "\n\t\t\t\t#   ZS HOSPITAL MANAGEMENT SYSTEM       #";
  cout &lt;&lt; "\n\t\t\t\t#########################################";
  cout &lt;&lt; "\n\n\n\n\n Press any key to continue......\n";
  system("cls");//Use to clear screen
  
  }
  
  /* ************************************************* Title Screen ********************************************* */
  void Title()//function for title screen
  {
  cout &lt;&lt; "\n\n\t\t----------------------------------------------------------------------------------";
  cout &lt;&lt; "\n\t\t\t\t       ZSc HOSPITAL         ";
  cout &lt;&lt; "\n\t\t----------------------------------------------------------------------------------";
  }
  
  
  /* ************************************************* Main Menu ********************************************* */
  void MainMenu()//function decleration
  {
  system("cls");
  int choose = 0;
  Title();// call Title function
  cout &lt;&lt; "\n\n\n\n\n\t\t\t\t1. Add Patients Record\n";
  cout &lt;&lt; "\n\t\t\t\t2. Delete Patients Record\n";
  cout &lt;&lt; "\n\t\t\t\t3. Search Patients Record\n";
  cout &lt;&lt; "\n\t\t\t\t4. Edit Patients Record\n";
  cout &lt;&lt; "\n\t\t\t\t5. list Patients Record\n";
  cout &lt;&lt; "\n\t\t\t\t6. search Patients by doctors name\n";
  cout &lt;&lt; "\n\t\t\t\t7. Exit\n";
  cout &lt;&lt; "\n\n\n \n\t\t\t\tChoose from 1 to 7:";
  cin &gt;&gt; choose;
  
  switch (choose)//switch to differeht case
  {
  
  case 1:
  Add_patient();//Add_rec function is called
  break;
  case 2:
  search_p();
  //Dlt_patient();//Dlt_rec function is call
  break;
  case 3:
  Search_rec();//View_rec function is call
  break;
  case 4:
  // Edit_rec();//Edit_rec function is call
  break;
  case 5:
  func_list();
  break;
  case 6:
  // ex_it();//ex_it function is call
  break;
  case 7:
  break;
  default:
  cout &lt;&lt; "\t\t\tInvalid entry. Please enter right option :)";
  system("pause");
  }//end of switch
  system("pause");
  
  }
  
  void LoginScreen()//function for login screen
  {
  //list of variables
  int e = 0;
  char Username[15];
  char Password[15];//cin.getline(Username,15);
  char original_Username[15] = "zain";
  char original_Password[15] = "123";
  
  do
  {
  cout &lt;&lt; "\n\n\n\n\t\t\t\tEnter your Username and Password :)";
  cout &lt;&lt; "\n\n\n\t\t\t\t\tUSERNAME:";
  cin.getline(Username, 15);
  cin.ignore();
  cout &lt;&lt; "\n\n\t\t\t\t\tPASSWORD:";
  cin.getline(Password, 15);
  //cin.ignore();
  
  
  if (strcmp(Username, original_Username) == 0 &amp;&amp; strcmp(Password, original_Password) == 0)//check if both the strings are equal and return 0 if equal
  {
  cout &lt;&lt; "\n\n\n\t\t\t\t\t...Login Successfull...";
  system("pause");
  MainMenu();
  break;
  }
  else
  {
  system("cls");
  cout &lt;&lt; "\n\t\t\tPassword in incorrect:&lt;&lt; Try Again :)";
  e++;
  }
  
  } while (e &lt;= 2);
  
  if (e&gt;2)
  {
  cout &lt;&lt; "You have cross the limit. You cannot login. : :(";
  //ex_it();
  }
  
  system("cls");
  }
  
  
  void Add_patient()
  {
  
  int c = 0, new_patient = 0;
  patient* obj = new patient();
  if (patient_head == NULL)
  {
  
  patient_head = obj;
  
  }
  else
  {
  
  obj-&gt;next = patient_head;
  obj-&gt;prev = NULL;
  patient_head = obj;
  }
  system("cls");
  Title();// call Title function
  //list of variables
  char ans;
  
  /****************************************id****************************/
  
  
  /* **************************First Name*********************************** */
  A:
  cout &lt;&lt; "\n\t\t\tFirst Name: ";
  cin &gt;&gt; obj-&gt;First_Name;
  obj-&gt;First_Name[0] = toupper(obj-&gt;First_Name[0]);
  int valid;
  
  if (strlen(obj-&gt;First_Name)&gt;20 || strlen(obj-&gt;First_Name)&lt;2)
  {
  cout &lt;&lt; "\n\t Invalid :( \t The max range for first name is 20 and min range is 2 :)";
  goto A;
  }
  else
  {
  for (int b = 0; (unsigned)b&lt;(unsigned)strlen(obj-&gt;First_Name); b++)
  {
  if (isalpha(obj-&gt;First_Name[b]))
  {
  valid = 1;
  }
  else
  {
  valid = 0;
  break;
  }
  }
  if (!valid)
  {
  cout &lt;&lt; "\n\t\t First name contain Invalid character :(  Enter again :)";
  goto A;
  }
  }
  
  /* ********************************************** Last name ************************************************ */
  B:
  cout &lt;&lt; "\n\t\t\tLast Name: ";
  cin &gt;&gt; obj-&gt;Last_Name;
  obj-&gt;Last_Name[0] = toupper(obj-&gt;Last_Name[0]);
  if (strlen(obj-&gt;Last_Name)&gt;20 || strlen(obj-&gt;Last_Name)&lt;2)//strlen to check length of char
  {
  cout &lt;&lt; "\n\t Invalid :( \t The max range for last name is 20 and min range is 2 :)";
  goto B;
  }
  else
  {
  for (int b = 0; (unsigned)b&lt;(unsigned)strlen(obj-&gt;Last_Name); b++)
  {
  if (isalpha(obj-&gt;Last_Name[b]))
  {
  valid = 1;
  }
  else
  {
  valid = 0;
  break;
  }
  }
  if (!valid)
  {
  cout &lt;&lt; "\n\t\t Last name contain Invalid character :(  Enter again :)";
  goto B;
  }
  }
  /* ******************************************* Gender ************************************************************** */
  int ok;
  
  do
  {
  cout &lt;&lt; "\n\t\t\tGender[F/M]: ";
  cin &gt;&gt; obj-&gt;Gender;
  if (toupper(obj-&gt;Gender) == 'M' || toupper(obj-&gt;Gender) == 'F')
  {
  ok = 1;
  }
  else
  {
  ok = 0;
  }
  if (!ok)
  {
  cout &lt;&lt; "\n\t\t Gender contain Invalid character :(  Enter either F or M :)";
  }
  } while (!ok);
  /* ***************************************** Age ********************************************************************** */
  cout &lt;&lt; "\n\t\t\tAge:";
  cin &gt;&gt; obj-&gt;age;
  /* **************************************** Address ******************************************************************* */
  do
  {
  C:
  cout &lt;&lt; "\n\t\t\tAddress: ";
  cin.getline(obj-&gt;Address, 80);
  cin.ignore(); //clear buffer before taking new
  obj-&gt;Address[0] = toupper(obj-&gt;Address[0]);
  
  if (strlen(obj-&gt;Address)&lt;4)
  {
  cout &lt;&lt; "\n\t Invalid :( \t The max range for address is 40 and min range is 4 :)";
  goto C;
  }
  
  } while (!valid);
  
  /* ******************************************* Contact no. ***************************************** */
  // char ch ;
  // ch = getchar();
  
  do
  {
  D:
  cout &lt;&lt; "\n\t\t\tContact no: ";
  cin &gt;&gt; obj-&gt;Contact_no;
  if (strlen(obj-&gt;Contact_no)&gt;11)
  {
  cout &lt;&lt; "\n\t Sorry :( Invalid. Contact no. must contain 11 numbers. Enter again :)";
  goto D;
  }
  else
  {
  for (int b = 0; (unsigned)b&lt;(unsigned)strlen(obj-&gt;Contact_no); b++)
  {
  if (!isalpha(obj-&gt;Contact_no[b]))
  {
  valid = 1;
  }
  else
  {
  valid = 0;
  break;
  }
  }
  if (!valid)
  {
  cout &lt;&lt; "\n\t\t Contact no. contain Invalid character :(  Enter again :)";
  goto D;
  }
  }
  } while (!valid);
  
  /* ************************************************** Email ******************************************** */
  do
  {
  cout &lt;&lt; "\n\t\t\tEmail: ";
  cin &gt;&gt; obj-&gt;Email;
  if (strlen(obj-&gt;Email)&gt;30 || strlen(obj-&gt;Email)&lt;8)
  {
  cout &lt;&lt; "\n\t Invalid :( \t The max range for email is 30 and min range is 8 :)";
  }
  } while (strlen(obj-&gt;Email)&gt;30 || strlen(obj-&gt;Email)&lt;8);
  
  /* ********************************************************* Problem *********************************************** */
  E:
  cout &lt;&lt; "\n\t\t\tProblem: ";
  cin &gt;&gt; obj-&gt;Problem;
  obj-&gt;Problem[0] = toupper(obj-&gt;Problem[0]);
  if (strlen(obj-&gt;Problem)&gt;15 || strlen(obj-&gt;Problem)&lt;3)
  {
  cout &lt;&lt; "\n\t Invalid :( \t The max range for first name is 15 and min range is 3 :)";
  goto E;
  }
  else
  {
  for (int b = 0; (unsigned)b&lt;(unsigned)strlen(obj-&gt;Problem); b++)
  {
  if (isalpha(obj-&gt;Problem[b]))//function in C which can be used to check if the passed character is an alphabet or not.
  {
  valid = 1;
  }
  else
  {
  valid = 0;
  break;
  }
  }
  if (!valid)
  {
  cout &lt;&lt; "\n\t\t Problem contain Invalid character :(  Enter again :)";
  goto E;
  }
  }
  
  /* ********************************************** Prescribed Doctor **************************************** */
  F:
  cout &lt;&lt; "\n\t\t\tPrescribed Doctor:";
  cin.getline(obj-&gt;Doctor, 50);
  obj-&gt;Doctor[0] = toupper(obj-&gt;Doctor[0]);
  if (strlen(obj-&gt;Doctor)&gt;30 || strlen(obj-&gt;Doctor)&lt;3)
  {
  cout &lt;&lt; "\n\t Invalid :( \t The max range for first name is 30 and min range is 3 :)";
  goto F;
  }
  else
  {
  for (int b = 0; (unsigned)b&lt;(unsigned)strlen(obj-&gt;Doctor); b++)
  {
  if (isalpha(obj-&gt;Doctor[b]))
  {
  valid = 1;
  }
  else
  {
  valid = 0;
  break;
  }
  }
  if (!valid)
  {
  cout &lt;&lt; "\n\t\t Doctor name contain Invalid character :(  Enter again :)";
  goto F;
  }
  }
  //**************************filing************************************
  ofstream myfile;
  myfile.open("text.txt", ios::app);//open file in write mode
  if (!myfile)
  {    // file couldn't be opened
  cout &lt;&lt; "Error: file could not be opened" &lt;&lt; endl;
  
  }
  else
  {
  
  myfile
  &lt;&lt; obj-&gt;Doctor &lt;&lt; "\n"
  &lt;&lt; obj-&gt;id &lt;&lt; "\n"
  &lt;&lt; obj-&gt;First_Name &lt;&lt; "\n"
  &lt;&lt; obj-&gt;Last_Name &lt;&lt; "\n"
  &lt;&lt; obj-&gt;Gender &lt;&lt; "\n"
  &lt;&lt; obj-&gt;age &lt;&lt; "\n"
  &lt;&lt; obj-&gt;Address &lt;&lt; "\n"
  &lt;&lt; obj-&gt;Contact_no &lt;&lt; "\n"
  &lt;&lt; obj-&gt;Email &lt;&lt; "\n"
  &lt;&lt; obj-&gt;Problem &lt;&lt; "\n"
  ;
  //&lt;&lt; date() &lt;&lt; "\n";
  
  }
  myfile.close();
  
  cout &lt;&lt; "\n\n\t\t\t.... Information Record Successful ...";
  sd:
  
  cin.ignore();
  cout &lt;&lt; "\n\n\t\t\tDo you want to add more[Y/N]?? ";
  cin &gt;&gt; ans;
  if (toupper(ans) == 'Y')
  {
  Add_patient();
  }
  else if (toupper(ans) == 'N')
  {
  cout &lt;&lt; "\n\t\t Thank you :) :)";
  MainMenu();
  }
  else
  {
  cout &lt;&lt; "\n\t\tInvalid Input\n";
  goto sd;
  }
  }
  
  
  
  
  
  void Search_rec(void)
  {
  char name[20];
  system("cls");
  Title();// call Title function
  patient* obj = new patient();
  
  fstream myfile;//open file to show on screen
  myfile.open("Record2.dat");
  if (!myfile)
  {    // file couldn't be opened
  cout &lt;&lt; "Error: file could not be opened" &lt;&lt; endl;
  
  }
  else
  {
  cout &lt;&lt; "\n\n\t\t\t!!!!!!!!!!!!!! Search Patients Record !!!!!!!!!!!!!\n";
  cout &lt;&lt; "\n Enter Patient Name to be viewed:";
  cin &gt;&gt; name;
  //fflush(stdin);
  //name[0] = toupper(name[0]);
  while (!EOF)
  {
  if (strcmp(obj-&gt;First_Name, name) == 0)
  {
  
  cout &lt;&lt; "\nFull Name";
  myfile &gt;&gt; obj-&gt;First_Name;
  myfile &gt;&gt; obj-&gt;Last_Name;
  cout &lt;&lt; obj-&gt;First_Name &lt;&lt; " " &lt;&lt; obj-&gt;Last_Name;
  cout &lt;&lt; "\nGender";
  myfile &gt;&gt; obj-&gt;Gender;
  cout &lt;&lt; obj-&gt;Gender;
  cout &lt;&lt; "\nAge";
  myfile &gt;&gt; obj-&gt;age;
  cout &lt;&lt; obj-&gt;age;
  cout &lt;&lt; "\nAddress";
  myfile &gt;&gt; obj-&gt;Address;
  cout &lt;&lt; "\nContact No.\n";
  myfile &gt;&gt; obj-&gt;Contact_no;
  cout &lt;&lt; "\nEmail";
  myfile &gt;&gt; obj-&gt;Email;
  cout &lt;&lt; "\nProblem";
  myfile &gt;&gt; obj-&gt;Problem;
  cout &lt;&lt; "\nPrescribed Doctor";
  myfile &lt;&lt; obj-&gt;Doctor;
  cout &lt;&lt; "================================== THANKS ==============================================================================";
  break;
  }
  }
  if (strcmp(obj-&gt;First_Name, name) != 0)
  {
  cout &lt;&lt; "Record not found!";
  system("pause");
  }
  }
  myfile.close();
  L:
  system("pause");
  cout &lt;&lt; "\n\n\t\t\tDo you want to view more[Y/N]??";
  char ans;
  cin &gt;&gt; ans;
  if (toupper(ans) == 'Y')
  {
  Search_rec();
  }
  else if (toupper(ans) == 'N')
  {
  cout &lt;&lt; "\n\t\t Thank you :) :)";
  MainMenu();
  }
  else
  {
  cout &lt;&lt; "\n\tInvalid Input.\n";
  goto L;
  }
  }
  
  /* **************************************VIEW RECORD*******************************************/
  void func_list()
  {
  
  ifstream myReadFile;
  myReadFile.open("text.txt");
  char output[100];
  if (!myReadFile)
  {
  cout &lt;&lt; "no file ";
  }
  while (!myReadFile.eof())
  {
  
  int r=0;
  myReadFile.getline(output, 100);
  
  // myReadFile &gt;&gt; output ;
  cout &lt;&lt; output &lt;&lt; "\n";
  r++;
  if (r == 10)
  {
  cout &lt;&lt; "\n\n";
  r = 0;
  }
  
  }
  myReadFile.close();
  cout &lt;&lt; "\n";
  system("pause");
  MainMenu();
  }
  
  
  ***void search_p()
  {
  int choice=0;
  char name[100];
  //M:
  cout &lt;&lt; "1 for   patients list and 2 for menu ";
  cin &gt;&gt; choice;
  if (choice == 1)
  {
  cout &lt;&lt; "\nenter your name ";
  cin &gt;&gt; name;
  ifstream myReadFile;
  myReadFile.open("text.txt");
  char output[100];
  if (!myReadFile)
  {
  cout &lt;&lt; "no file ";
  }
  while (!myReadFile.eof())
  {
  int r = 0;
  myReadFile.getline(output, 100);
  if (output == name)
  {
  while (r == 10)
  {
  myReadFile.getline(output, 100);
  r++;
  }
  r = 0;
  }
  // myReadFile &gt;&gt; output ;
  cout &lt;&lt; output &lt;&lt; "\n";
  r++;
  if (r == 10)
  {
  cout &lt;&lt; "\n";
  r = 0;
  }
  }
  myReadFile.close();
  cout &lt;&lt; "\n";
  MainMenu();
  //system("pause");
  //goto M;
  }
  else
  {
  MainMenu();
  }
  }***
  
  
  
  
  int main()
  {
  
  WelcomeScreen();//Use to call WelcomeScreen function
  Title();//Use to call Title function
  LoginScreen();//Use to call Menu function
  }
  </code></pre>