Help with creating a search function

Okay so I am posting a new thread because the other one I made was kind of on the wrong track.

I am creating a family tree program. I need functions to add stuff to it and search for people inside it. Right now I am in the process of testing various functions to see if they do what I want.

I am currently trying to make a search function.

Here is my code thus far (not separated by multiple files yet, purely for testing)

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
#include <iostream>
#include <string>
using namespace std;

const string EMPTY = "EMPTY";

class person
{
        string name;
        string birthdate;
        char marital;
        person *spouse;
        person *kids;
        person *sibling;
        string deathdate;
    public:
        person ();
        void set_name (string input)
        {
            name = input;
        };
        void set_birthdate (string input)
        {
            birthdate = input;
        };
        void set_marital (char input)
        {
            marital = input;
        };
        void set_spouse()
        {
            spouse = new(person);
        };
        void set_spouse_name(string input)
        {
            spouse->name = input;
        };
        void set_kids()
        {
            kids = new(person);
        };
        void set_kids_name(string input)
        {
            kids->name = input;
        };
        void set_sibling()
        {
            sibling = new(person);
        };
        void print_all();
        bool search(string input, person *head);

};

person::person()
{
    name = EMPTY;
    birthdate = EMPTY;
    marital = ' ';
    spouse = NULL;
    kids = NULL;
    sibling = NULL;
    deathdate = EMPTY;
}

void person::print_all()
{
    cout << name << endl << birthdate << endl << marital << endl << spouse->name << endl << kids->name << endl;
}


bool person::search(string input, person *head)
{
    if (head == NULL)
        return false;
    if (head->name == input)
    {
        head->print_all();
        return true;
    }
    if (search(input, spouse))
        return true;
    else if (search (input, kids))
        return true;
    else if (search (input, sibling))
        return true;
    else return false;
}

int main() {

person *person1 = new(person);

person1->set_name("Frank");
person1->set_birthdate("12MARCH1975");
person1->set_marital ('M');
person1->set_spouse();
person1->set_spouse_name("Mary");
person1->set_kids();
person1->set_kids_name("Frank JR");
person1->print_all();
person1->search("Frank JR", person1);



return 0;
}



Currently when I run that search function in the main, I get a seg. fault.

I am trying to figure out the best way to have it search through my family tree. It needs to check the initial person, their spouse, their siblings, their kids, then it needs to check all of the above for their kids and so on.

What am I doing wrong?
Last edited on
Topic archived. No new replies allowed.