So for my final class project, we have to create a family tree. Normally it wouldn't seem too bad but the way the professor has worded it and the way the inputs are being doing has got me straight confused.
Here is the project description:
Your fourth project is to write a program to build and display a family tree.
I want you to use a class to hold the needed data for each person and pointers to the people associated
with each person [wife and children].
The input for the people consists of a name (with a trailing distinguishing number; treat the
number as a part of the name), a birthdate, a marital status (either ‘M’ or ‘S’), a wife name
and birthdate if the marital status is ‘M’, a variable number of children names, a possible
death date (which will start with a digit, so you can distinguish it from a child name), and
a trailing ‘X’.
zB:
Steve1 12Nov1931 M Mary1 17Dec1833 Steve2 Harry1 Sue1 3Jan1959 X
Steve2 31Dec1957 S Xavier1 X
After the people, there will be the word ‘QUIT’ and a series of names (possibly a spouse
name).
For each name, I want you to print that person, in parentheses his/her birthdate),
if married the string ” x ” (with the spaces) his/her spouse and their birthdate, and on
succeeding indented lines the children and their birthdates (one per line).
Handling the inputs is giving me the biggest nightmare right now. I had some pseudo code going, like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Create object
while (cin >> data):
head points to the created object:
is name empty? if so, fill it
is dob empty? if so, fill it
is marital empty? if so, fill it
if marital is S and the next input is a string that begins with a letter, then person->children = new(kids) and
if person->children.name is empty, fill it else person->children->next = new(kids) and person->children
if marital is S and the next input is a string that begins with an int, then if person.dod is empty, fill it.
if marital is M and person->spoouse == NULL,
My problem is this: I have my class and struct defined as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Person
{
string name;
string dob;
string marital;
string dod;
Person *spouse;
kids *children;
public:
void ft_add(...); //function to add people to the family tree
Person* ft_search(...); //function to search for people in the family tree
void ft_print(...); //function which will take the ptr returned from search and print data from it
};
struct kids
{
Person *child;
kids *next;
};
I learned just the other day that you CAN'T have an object inside of itself...obviously that wouldn't make sense. So I have to use a pointer to an object of type person within the kids struct instead. So for my pseudo code above, I can't quite figure out how I would allocate memory for the struct inside the initial object, then initialize a person object inside of the struct, then access that objects data fields.
Over all I just am running into walls left and right on this assignment. If anyone could provide guidance, I would appreciate it.
struct kids; // This tells the compile that struct kids exists which is defined later (you can now use a pointer/reference to it)
class Person
{
string name;
string dob;
string marital;
string dod;
Person *spouse;
kids *children;
public:
void ft_add(...); //function to add people to the family tree
Person* ft_search(...); //function to search for people in the family tree
void ft_print(...); //function which will take the ptr returned from search and print data from it
};
struct kids
{
Person *child;
kids *next;
};
Thanks coder. However can you help me figure out a specific piece of syntax?
For example,
if marital is S and the next input is a string that begins with a letter, then person->children = new(kids) and
if person->children.name is empty, fill it else person->children->next = new(kids) and person->children
this was kinda iffy I felt. But basically, I just want to brute force the inputs. So I am just going to have several IF checks and maybe some string manip but the biggest thing is I am not sure:
1) WHERE the struct children should be allocated in my big IF checking function. If I allocate kids only AFTER marital = M, then that messes me up if someone is single but still has kids. If I allocate kids sooner, someone might not have kids, etc.
2) HOW would I actually go about doing this? Would it be like
enum State
{
Name,
Birthdate,
MaritalStatus,
WifeName,
WifeBirthdate,
ChildrenNameOrDeathDateOrEndOfRecord
};
...
State state = Name;
Person p = new Person();
while(cin >> data)
{
switch(state)
{
case Name:
p->name = data;
state = Birthdate;
break;
case Birthdate:
...
}
}
you may put the while in a read function of Person.
I'd suggest following: Read the name outside the person ("QUIT", eof). The state machine inside.
That above is an example. Hope you get the float...