1

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):

#include<iostream>
#include <fstream>
#include<ctime>
#include<conio.h>
#include<string.h>
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 << "\n\n\n\n\n\n\n\t\t\t\t#########################################";
cout << "\n\t\t\t\t#\t\t WELCOME TO             #";
cout << "\n\t\t\t\t#   ZS HOSPITAL MANAGEMENT SYSTEM       #";
cout << "\n\t\t\t\t#########################################";
cout << "\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 << "\n\n\t\t----------------------------------------------------------------------------------";
cout << "\n\t\t\t\t       ZSc HOSPITAL         ";
cout << "\n\t\t----------------------------------------------------------------------------------";
}


/* ************************************************* Main Menu ********************************************* */
void MainMenu()//function decleration
{
system("cls");
int choose = 0;
Title();// call Title function
cout << "\n\n\n\n\n\t\t\t\t1. Add Patients Record\n";
cout << "\n\t\t\t\t2. Delete Patients Record\n";
cout << "\n\t\t\t\t3. Search Patients Record\n";
cout << "\n\t\t\t\t4. Edit Patients Record\n";
cout << "\n\t\t\t\t5. list Patients Record\n";
cout << "\n\t\t\t\t6. search Patients by doctors name\n";
cout << "\n\t\t\t\t7. Exit\n";
cout << "\n\n\n \n\t\t\t\tChoose from 1 to 7:";
cin >> 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 << "\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 << "\n\n\n\n\t\t\t\tEnter your Username and Password :)";
cout << "\n\n\n\t\t\t\t\tUSERNAME:";
cin.getline(Username, 15);
cin.ignore();
cout << "\n\n\t\t\t\t\tPASSWORD:";
cin.getline(Password, 15);
//cin.ignore();


if (strcmp(Username, original_Username) == 0 && strcmp(Password, original_Password) == 0)//check if both the strings are equal and return 0 if equal
{
cout << "\n\n\n\t\t\t\t\t...Login Successfull...";
system("pause");
MainMenu();
break;
}
else
{
system("cls");
cout << "\n\t\t\tPassword in incorrect:<< Try Again :)";
e++;
}

} while (e <= 2);

if (e>2)
{
cout << "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->next = patient_head;
obj->prev = NULL;
patient_head = obj;
}
system("cls");
Title();// call Title function
//list of variables
char ans;

/****************************************id****************************/


/* **************************First Name*********************************** */
A:
cout << "\n\t\t\tFirst Name: ";
cin >> obj->First_Name;
obj->First_Name[0] = toupper(obj->First_Name[0]);
int valid;

if (strlen(obj->First_Name)>20 || strlen(obj->First_Name)<2)
{
cout << "\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<(unsigned)strlen(obj->First_Name); b++)
{
if (isalpha(obj->First_Name[b]))
{
valid = 1;
}
else
{
valid = 0;
break;
}
}
if (!valid)
{
cout << "\n\t\t First name contain Invalid character :(  Enter again :)";
goto A;
}
}

/* ********************************************** Last name ************************************************ */
B:
cout << "\n\t\t\tLast Name: ";
cin >> obj->Last_Name;
obj->Last_Name[0] = toupper(obj->Last_Name[0]);
if (strlen(obj->Last_Name)>20 || strlen(obj->Last_Name)<2)//strlen to check length of char
{
cout << "\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<(unsigned)strlen(obj->Last_Name); b++)
{
if (isalpha(obj->Last_Name[b]))
{
valid = 1;
}
else
{
valid = 0;
break;
}
}
if (!valid)
{
cout << "\n\t\t Last name contain Invalid character :(  Enter again :)";
goto B;
}
}
/* ******************************************* Gender ************************************************************** */
int ok;

do
{
cout << "\n\t\t\tGender[F/M]: ";
cin >> obj->Gender;
if (toupper(obj->Gender) == 'M' || toupper(obj->Gender) == 'F')
{
ok = 1;
}
else
{
ok = 0;
}
if (!ok)
{
cout << "\n\t\t Gender contain Invalid character :(  Enter either F or M :)";
}
} while (!ok);
/* ***************************************** Age ********************************************************************** */
cout << "\n\t\t\tAge:";
cin >> obj->age;
/* **************************************** Address ******************************************************************* */
do
{
C:
cout << "\n\t\t\tAddress: ";
cin.getline(obj->Address, 80);
cin.ignore(); //clear buffer before taking new
obj->Address[0] = toupper(obj->Address[0]);

if (strlen(obj->Address)<4)
{
cout << "\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 << "\n\t\t\tContact no: ";
cin >> obj->Contact_no;
if (strlen(obj->Contact_no)>11)
{
cout << "\n\t Sorry :( Invalid. Contact no. must contain 11 numbers. Enter again :)";
goto D;
}
else
{
for (int b = 0; (unsigned)b<(unsigned)strlen(obj->Contact_no); b++)
{
if (!isalpha(obj->Contact_no[b]))
{
valid = 1;
}
else
{
valid = 0;
break;
}
}
if (!valid)
{
cout << "\n\t\t Contact no. contain Invalid character :(  Enter again :)";
goto D;
}
}
} while (!valid);

/* ************************************************** Email ******************************************** */
do
{
cout << "\n\t\t\tEmail: ";
cin >> obj->Email;
if (strlen(obj->Email)>30 || strlen(obj->Email)<8)
{
cout << "\n\t Invalid :( \t The max range for email is 30 and min range is 8 :)";
}
} while (strlen(obj->Email)>30 || strlen(obj->Email)<8);

/* ********************************************************* Problem *********************************************** */
E:
cout << "\n\t\t\tProblem: ";
cin >> obj->Problem;
obj->Problem[0] = toupper(obj->Problem[0]);
if (strlen(obj->Problem)>15 || strlen(obj->Problem)<3)
{
cout << "\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<(unsigned)strlen(obj->Problem); b++)
{
if (isalpha(obj->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 << "\n\t\t Problem contain Invalid character :(  Enter again :)";
goto E;
}
}

/* ********************************************** Prescribed Doctor **************************************** */
F:
cout << "\n\t\t\tPrescribed Doctor:";
cin.getline(obj->Doctor, 50);
obj->Doctor[0] = toupper(obj->Doctor[0]);
if (strlen(obj->Doctor)>30 || strlen(obj->Doctor)<3)
{
cout << "\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<(unsigned)strlen(obj->Doctor); b++)
{
if (isalpha(obj->Doctor[b]))
{
valid = 1;
}
else
{
valid = 0;
break;
}
}
if (!valid)
{
cout << "\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 << "Error: file could not be opened" << endl;

}
else
{

myfile
<< obj->Doctor << "\n"
<< obj->id << "\n"
<< obj->First_Name << "\n"
<< obj->Last_Name << "\n"
<< obj->Gender << "\n"
<< obj->age << "\n"
<< obj->Address << "\n"
<< obj->Contact_no << "\n"
<< obj->Email << "\n"
<< obj->Problem << "\n"
;
//<< date() << "\n";

}
myfile.close();

cout << "\n\n\t\t\t.... Information Record Successful ...";
sd:

cin.ignore();
cout << "\n\n\t\t\tDo you want to add more[Y/N]?? ";
cin >> ans;
if (toupper(ans) == 'Y')
{
Add_patient();
}
else if (toupper(ans) == 'N')
{
cout << "\n\t\t Thank you :) :)";
MainMenu();
}
else
{
cout << "\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 << "Error: file could not be opened" << endl;

}
else
{
cout << "\n\n\t\t\t!!!!!!!!!!!!!! Search Patients Record !!!!!!!!!!!!!\n";
cout << "\n Enter Patient Name to be viewed:";
cin >> name;
//fflush(stdin);
//name[0] = toupper(name[0]);
while (!EOF)
{
if (strcmp(obj->First_Name, name) == 0)
{

cout << "\nFull Name";
myfile >> obj->First_Name;
myfile >> obj->Last_Name;
cout << obj->First_Name << " " << obj->Last_Name;
cout << "\nGender";
myfile >> obj->Gender;
cout << obj->Gender;
cout << "\nAge";
myfile >> obj->age;
cout << obj->age;
cout << "\nAddress";
myfile >> obj->Address;
cout << "\nContact No.\n";
myfile >> obj->Contact_no;
cout << "\nEmail";
myfile >> obj->Email;
cout << "\nProblem";
myfile >> obj->Problem;
cout << "\nPrescribed Doctor";
myfile << obj->Doctor;
cout << "================================== THANKS ==============================================================================";
break;
}
}
if (strcmp(obj->First_Name, name) != 0)
{
cout << "Record not found!";
system("pause");
}
}
myfile.close();
L:
system("pause");
cout << "\n\n\t\t\tDo you want to view more[Y/N]??";
char ans;
cin >> ans;
if (toupper(ans) == 'Y')
{
Search_rec();
}
else if (toupper(ans) == 'N')
{
cout << "\n\t\t Thank you :) :)";
MainMenu();
}
else
{
cout << "\n\tInvalid Input.\n";
goto L;
}
}

/* **************************************VIEW RECORD*******************************************/
void func_list()
{

ifstream myReadFile;
myReadFile.open("text.txt");
char output[100];
if (!myReadFile)
{
cout << "no file ";
}
while (!myReadFile.eof())
{

int r=0;
myReadFile.getline(output, 100);

// myReadFile >> output ;
cout << output << "\n";
r++;
if (r == 10)
{
cout << "\n\n";
r = 0;
}

}
myReadFile.close();
cout << "\n";
system("pause");
MainMenu();
}


***void search_p()
{
int choice=0;
char name[100];
//M:
cout << "1 for   patients list and 2 for menu ";
cin >> choice;
if (choice == 1)
{
cout << "\nenter your name ";
cin >> name;
ifstream myReadFile;
myReadFile.open("text.txt");
char output[100];
if (!myReadFile)
{
cout << "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 >> output ;
cout << output << "\n";
r++;
if (r == 10)
{
cout << "\n";
r = 0;
}
}
myReadFile.close();
cout << "\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
}
  • 5
    Start by replacing all your fixed size `char` arrays with `std::string`. There's a comparison in `search_p` that won't work, because you can't compare character arrays with `==`, which will compare their _addresses_. You need to use `strcmp` or something similar. Using `std::string` the `==` comparison would Just Work. – 1201ProgramAlarm Dec 25 '19 at 17:26
  • Key lesson on reading data: *always* check *after* the attempt to read data that it was successful. Using any variable attempted to be read prior to this check can yield bad data and can be a security threat. With respect to the actual question: reduce the code to show only what the problem is rather than posting all the code. – Dietmar Kühl Dec 25 '19 at 18:14
  • Prefer to use `std::list` rather than implementing your own. The `std::list` has already been validated. – Thomas Matthews Dec 25 '19 at 18:34
  • That's an aweful lot of unindented code, please try to shorten and format it if possible. Maybe you also want to get a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn some basic concepts like the use of STL-Containers, `std::string`, smart pointers or [Why “using namespace std;” is considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Also you should avoid `goto`. – Lukas-T Dec 25 '19 at 20:02

0 Answers0