Confusion:Comparing a Node's String variable in a function

Let's say i have this node:
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;


Now I want to create a funtion which would take in Patient ID (pid) and then traverse through the list and display the desired patient's details.

I am inserting each node at the beggining of the linked list here is the full code,it also includes the ListPatient() function,which concerns the questions.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<string>
#include<iostream>
#include<fstream>
#include<windows.h>
#include<cstdio>
#include<iomanip>
#include<process.h>
using namespace std;
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;
class HospitalFunctions
{
public:
    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!";
}
void ListPatient()
{
    string S_ID,S_ID2;
    cout << "\nPatient ID:";
    fflush(stdin);
    getline(cin,S_ID);
    Patient *disp = head;
    while(disp->next!=NULL)
    {
        {   system("cls");
            cout << setw(30) << "\nName:" << disp->pname;
            cout << endl << setw(30) <<"ID:" << disp->pid;
            cout << endl << setw(30) << "Admit Date:" << disp->indate;
            cout << endl << setw(30) << "Discharge Date:" << disp->outdate;
            cout << endl << setw(30) << "Discomfort:" << disp->disease;
            cout <<endl  << setw(30)  << "Department:" << disp->department;
            cout << endl << setw(30) << "Contact:" << disp->phonenum;
            break;
        }
        else
            disp= disp->next;
    }
}
};
int main()
{
    HospitalFunctions H;
    char ans ='y';
    int choice;
do
{
    system("color 09");
    cout.width(108);
    cout << "Hospital Care Software\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 << "\n\nEnter 'Y' Return TO Main:";
                cin >> ans;  system("cls");
                break;
        case 4: H.ListPatient();
                cout << "\n\nEnter 'Y' To Return TO Main:";
                cin >> ans;  system("cls");
                break;
        case 9: exit(1);
                break;
        default:cout << "\nInvalid Selection,Try Again!";
                Sleep(800);
                system("cls");
                break;
    }
}   while(ans=='y');
return 0;
}


I tried to create like two string variables and thought to copy PID constantly into another and then break the loop it matches,but it didn't work. strcmp() and strcpy() will ofcourse not work,these are strings not character arrays and i couldn't find a solution to my problems in the string library,do i have to convert it into a character then copy and then traverse and find? or there is some thing i could do with keeping my string variables intact.

1
2
3
4
5
6
7
8
9
10
11
void foo( const std::string & pid )
{
  Patient* disp = head;
  while ( disp ) {
    if ( pid == disp->pid ) {
      std::cout << *disp; // requires operator<< (ostream&, const Patient&)
      break; // stops on first match
    }
    disp = disp->next;
  }
}
Topic archived. No new replies allowed.