Linked List Display Function Not Working.
Sep 2, 2018 at 10:45am UTC
Hey Guys,This Function is not working as expected,It will not display disp.pname
The lines before the structure pointer declaration are working fine.
I have tried to insert nodes at only the beginning.
Function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void ListPatients()
{
system("cls" );
cout << "Name" <<setw(8) << "ID" << setw(18) << "Phone Number" << setw(20) << "Department" << setw(18) << "Doctor" << setw(16) << "Disease" << setw(18)
<<"Admit Date" << setw(18) <<"Discharge Date\n" ;
cout <<"------------------------------------------------------------------------------------------------------------------------" ;
Patient *disp = head;
while (disp!=NULL)
{
cout << disp->pname;
disp = disp->next;
}
}
Structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
struct Patient
{
string pname;
string pid;
string phonenum;
string address;
string fathersname;
string department;
string disease;
string allergies;
string indate;
string outdate;
Patient *next;
}*head;
Insert Function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
void GetDetails()
{
Patient *ptr = new Patient;
ptr->next = NULL;
system("cls" );
fflush(stdin);
cout << "\nEnter Patient's Name:" ;
getline(cin,ptr->pname);
fflush(stdin);
cout << "\nEnter Patient ID:" ;
getline(cin,ptr->pid);
fflush(stdin);
cout << "\nEnter Patient's Phone Number:" ;
getline(cin,ptr->phonenum);
fflush(stdin);
cout << "\nEnter The Patient's Father|Mother|Person Who Admitted Name:" ;
getline(cin,ptr->fathersname);
fflush(stdin);
cout << "\nEnter The Admit Date:" ;
getline(cin,ptr->indate);
fflush(stdin);
cout << "\nEnter The Discharge Date(NULL,If Not Discharged):" ;
getline(cin,ptr->outdate);
fflush(stdin);
cout << "\nEnter The Department:" ;
getline(cin,ptr->department);
fflush(stdin);
cout << "\nAllergies(If Any):" ;
getline(cin,ptr->allergies);
fflush(stdin);
cout << "\nEnter The Disease/Discomfort Being Experienced by the patient:" ;
getline(cin,ptr->disease);
fflush(stdin);
cout << "\nEnter The Patient's Address:" ;
getline(cin,ptr->address);
fflush(stdin);
cout << "\nRegistering Patient...." ;
if (head == NULL)
{
head = ptr;
}
else
if (head!=NULL)
{
ptr->next=head;
head = ptr;
}
Sleep(2000);
cout << "\nPatient Registered!" ;
}
Main Function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
int main()
{
head = NULL;
HospitalFunctions H;
int choice;
system("color 09" );
cout.width(108);
cout << "Hospital Care\n" ;
cout.width(110);
cout << "--------------------------\n" ;
cout.width(100);
cout << "MENU:\n" ;
cout << "\n1.Add A Patient\n2.OPD Appointment\n3.Show Charges\n4.View Patient Details\n5.Modify Patient Details\n6.Emergency\n7.Customer Care\n8.List All Patients\n9.Instruction Manual\n10.EXIT" ;
cout << endl << endl << "Enter Your Choice(1-9):" ;
cin >> choice;
switch (choice)
{
case 1: H.GetDetails();
cout << "\nPress Enter To Return TO Main" ;
cin.get(); system("cls" ); main();
break ;
case 5: H.ListPatients();
cout << "\nPress Enter To Return TO Main" ;
cin.ignore(); cin.get(); system("cls" ); main();
break ;
case 9: exit(1);
default :cout << "\nInvalid Selection,Try Again!" ;
Sleep(800);
system("cls" );
main();
break ;
}
return 0;
}
The Whole Code Is pretty big and messy for right now i tried to include only the parts i think might have some errors.
Last edited on Sep 2, 2018 at 10:52am UTC
Sep 2, 2018 at 2:19pm UTC
You can't call main recursively. Use a loop instead. It's entirely possible that this is causing the problem.
Add a newline after printing each patient's name.
You have head declared in main, but I assume there is also a head declared in HospitalFunctions. The one used in GetDetails and ListPatients isn't the one declared in main.
Topic archived. No new replies allowed.