Printing contents of vectors in a linked list

Hi there, I'm still working on the same family tree program and converting it into a linked list (next improvements will be virtual functions); but I'm having trouble with displaying the children vector. Ideally, I want the final output to display as ...

Child of Abraham
1. Sarah
.... Issac
2. Keturah
.... Jokshan
.... Midian
3. Hagar
.... Ishmael

Abraham has three wives: Sarah, Keturah, and Hagar. Issac is child of Abraham and Sarah. Jokshan and Midian are child(s) of Abraham and Keturah. Ishmael is child of Abraham and Hagar.

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdlib.h>
#include <string>
#include <iostream>
#include <vector>

using namespace std;

class Person
{
    public:
        struct familyNode
        {
            string name;
            int ID;
            string gender;
            familyNode *next;
        };
        vector <string> children;
        familyNode *head = nullptr;
        familyNode *lastNode = nullptr;
    public:
        Person ();
        void append (string item, int id, string gen);
        // void append (string item, vector <int> vec);
        void display ();

};

class Male : public Person
{
    public:
        vector <string> wives;
        void addWife (string p1, string p2);
        void displayWifeChildren (string p1);
};

class Female: public Person
{
    public:
         vector <string> husbands;
         void addChild (string p2, string p3);
};

Person::Person ()
{

}

void Person:: append (string item, int id, string gen)
{
    familyNode* newNode;
    newNode = new familyNode;
    newNode -> name = item;
    newNode -> ID = id;
    newNode -> gender = gen;
    int count = 0;

    if (head == nullptr)
    {
        head = newNode;
        lastNode = newNode;
        count++;
    }
    else
    {
        lastNode -> next = newNode;
        lastNode = lastNode -> next;
        count ++;
    }
}

void Person::display ()
{
    if (head == nullptr)
    {
         cout << "\nThere are no people in the list.\n";
    }
    else
    {
        familyNode *p = head;
        cout << "Name" << "\t" << "ID" << "\t" << "Gender\n";
        while (p != nullptr)
        {
            cout << p -> name << "\t" << p -> ID << "\t" << p -> gender << "\n";
            p = p-> next;
        }
    }
}

void Male:: addWife (string p1, string p2)
{
    wives. push_back (p2);
}

void Male:: displayWifeChildren (string p1)
{
    familyNode* newNode;
    newNode = new familyNode;

    cout << "Child of " << p1 << "\n";

    for (int i = 0; i < wives.size (); i++)
    {
        cout << i +1 << " " << wives [i] << "\n";

        if (newNode -> name == wives [i])
        {
            for (int j = 0; j < children.size (); j++)
            {
                  cout << "\t"<< children [j] << "\n";
            }
        }
    }
}

void Female:: addChild  (string p2, string p3)
{
    children. push_back (p3);
}

int main ()
{
    Person per;
    Male mal;
    Female fem;

    cout << "\nThe people in the system are ... \n";

    per. append ("Abraham", 1, "Male");
    per. append ("Sarah", 2, "Female");
    per. append ("Keturah", 3, "Female");
    per. append ("Hagar", 4, "Female");
    per. append ("Isaac", 5, "Male");
    per. append ("Jokshan", 6, "Male");
    per. append ("Midian", 7, "Male");
    per. append ("Ishmael", 8, "Male");
    per. append ("Rebekah", 9, "Female");
    per. append ("Esau", 10, "Male");

    per. display ();
    cout << "\n";

    mal. addWife ("Abraham", "Sarah");
    mal. addWife ("Abraham", "Keturah");
    mal. addWife ("Abraham", "Hagar");

    fem. addChild ("Sarah", "Isaac");
    fem. addChild ("Keturah", "Jokshan");
    fem. addChild ("Keturah", "Midian");

    fem. addChild ("Hagar", "Ishmael");

    mal. displayWifeChildren ("Abraham");
}
Last edited on
1
2
3
4
void Male:: displayWifeChildren (string p1)
{
    familyNode* newNode;
    newNode = new familyNode;
¿? ¿why is a print function creating nodes?
you later do if (newNode -> name == wives [i]), you just created `newNode' ¿what do you expect it to contain?

I don't understand the purpose of your classes, it seems that you are confusing an item with a collection.
void Female:: addChild (string p2, string p3) you never use `p2' (the name of the wife) so don't understand how do you expect that to work.
Topic archived. No new replies allowed.