question

hello every body , I 'm in trouble , i have a project about clinic by using borland c++ , my problem is that i want to search in the project of clinic by
serial number or ID of each patient and when the search ended , it must be appeared the application of this patient (Name , age , sex , .......)
this application that patient fill it when he come to the clinic in the first time . and thank you.
my question that how to connect the application with ID or name
How are patients represented in your program? A good way would be using a struct:

1
2
3
4
5
6
7
8
9
10
struct Patient
{
    int id;
    string name;
    int age;
    char sex;

    //more stuff here...

};

And what is a clinic? Is it just a container of patients, like, let's say, vector<Patient> ? If so, you can use stl algorithms to do what you want (find looks good to me, but you'll have to define a Patient==intPatient operator to use it. Alternatively, you could do the searching on your own using the access methods your container provides).

http://cplusplus.com/doc/tutorial/structures/

http://cplusplus.com/reference/stl/
http://cplusplus.com/reference/stl/vector/

http://cplusplus.com/reference/algorithm/
http://cplusplus.com/reference/algorithm/find/
Last edited on
Ok, here's an example:

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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> //for find
using namespace std;

struct Patient
{
    int id;
    string name;
    int days_in;

    Patient(int id_,const char * name_,int days_in_):
        id(id_),name(name_),days_in(days_in_){}

    //two patients are considered equal
    //if-f they have the same id
    bool operator==(const Patient & p)
    {
        return id==p.id;
    }
};

int main()
{
    vector<Patient> clinic;

    clinic.push_back(Patient(1,"Bob",2));
    clinic.push_back(Patient(2,"Tom",5));
    clinic.push_back(Patient(3,"Pam",12));
    clinic.push_back(Patient(4,"Sam",4));
    clinic.push_back(Patient(5,"Liz",7));

    //searching for patient with id==2
    Patient patient(2,"",0);

    cout << find(clinic.begin(),clinic.end(),patient)->name << endl;

    cin.get();
    return 0;
}

This approach works ok in most of the cases where you have something like a unique id member for a data structure, but you may face situations where this isn't useful. What if you wanted to also search by some other field of the record, for example by name? You can't redefine the == operator. How would you do that then? A good solution would be function objects:

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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> //for find_if
using namespace std;

struct Patient
{
    int id;
    string name;
    int days_in;

    Patient(int id_,const char * name_,int days_in_):
        id(id_),name(name_),days_in(days_in_){}

    struct HasId
    {
        int id;

        HasId(int id_):id(id_){}

        bool operator()(const Patient & p)
        {
            return p.id==id;
        }
    };

    struct HasName
    {
        string name;

        HasName(string name_):name(name_){}

        bool operator()(const Patient & p)
        {
            return p.name==name;
        }
    };
};

int main()
{
    vector<Patient> clinic;

    clinic.push_back(Patient(1,"Bob",2));
    clinic.push_back(Patient(2,"Tom",5));
    clinic.push_back(Patient(3,"Pam",12));
    clinic.push_back(Patient(4,"Sam",4));
    clinic.push_back(Patient(5,"Liz",7));

    //let's see the name of the patient who has an id of 3
    cout << find_if(clinic.begin(),clinic.end(),Patient::HasId(3))->name << endl;

    //let's see the id of the patient named "Liz"
    cout << find_if(clinic.begin(),clinic.end(),Patient::HasName("Liz"))->id << endl;

    cin.get();
    return 0;
}

For more examples with stl containers and algorithms, and function objects, check this out:
http://cplusplus.com/forum/articles/10879/

Last edited on
Topic archived. No new replies allowed.