family tree

This is a discussion on family tree within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get my code to indentthe output for this tree, but don't know how to approach it. Can ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Question family tree

    I'm trying to get my code to indentthe output for this tree, but
    don't know how to approach it. Can some one take a look at it, and make some ajustment or suggestions.
    Example: The children should be indented next to their parents, and they to their parents etc...

    Thanks!

    //Code Below

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    using namespace std;

    class CPerson
    {
    public:
    CPerson( string name );

    // The data should really be private, but to allow
    // you to concentrate on vectors and pointers,
    // we'll leave them public for now.

    CPerson * m_father;
    CPerson * m_mother;
    string m_name;
    vector< CPerson * > m_children;
    };


    CPerson::CPerson( string name )
    {
    m_father = 0;
    m_mother = 0;
    m_name = name;
    // vector is initialized by default constructor
    }

    vector< CPerson * > everybody;
    // a list of pointers to every CPerson object that we have

    CPerson * find_person( string name )
    {
    // Find the CPerson object with the specified name,
    // and return a pointer to it.
    // If the name is not found, return 0.

    int i;
    for( i = 0; i < everybody.size(); i++ )
    {
    CPerson * person = everybody.at( i );
    if( person->m_name == name )
    {
    return person;
    }
    }

    // name not found

    return 0;
    }

    CPerson * find_or_add_person( string name )
    {
    // Like find_person, but if no matching object
    // is found it creates a new one, so it never
    // returns 0.

    CPerson * person = find_person( name );
    if( person == 0 )
    {
    person = new CPerson( name );
    everybody.push_back( person );
    }
    return person;
    }

    void add_relationship(
    string child_name,
    string relationship,
    string parent_name )
    {
    // Add the specified relationship into the data
    // structure. If any name has not been seen
    // before, create a new node for it.

    CPerson * child = find_or_add_person( child_name );
    CPerson * parent = find_or_add_person( parent_name );

    if( relationship == "FATHER" )
    {
    child->m_father = parent;
    parent->m_children.push_back( child );
    }
    else if( relationship == "MOTHER" )
    {
    child->m_mother = parent;
    parent->m_children.push_back( child );
    }
    else
    {
    cerr << "illegal relationship " << relationship << "\n";
    }
    }

    void read_all( istream & input )
    {
    // Read a file full of entries that look like:
    // ChildsName FATHER DadsName
    // or:
    // ChildsName MOTHER MomsName

    string child_name;
    string relationship;
    string parent_name;

    while( input >> child_name >> relationship >> parent_name )
    {
    add_relationship( child_name, relationship, parent_name );
    }
    }

    void dump_person( ostream & output, CPerson * person )
    {
    // Dump everything we know about one specified person.

    output << "Name: " << person->m_name << "\n";

    CPerson * father = person->m_father;
    if( father != 0 )
    {
    output << " Father: " << father->m_name << "\n";
    }

    CPerson * mother = person->m_mother;
    if( mother != 0 )
    {
    output << " Mother: " << mother->m_name << "\n";
    }

    output << " Children:";
    int i;
    for( i = 0; i < person->m_children.size(); i++ )
    {
    output << " " << person->m_children.at( i )->m_name;
    }
    output << "\n";
    }

    void dump_all( ostream & output )
    {
    // Dump the entire data structure (for testing
    // and debugging).

    int i;
    for( i = 0; i < everybody.size(); i++ )
    {
    dump_person( output, everybody.at( i ) );
    }
    }

    int main()
    {
    // Read a whole family tree.

    ifstream input;
    input.open( "family.txt" );
    if( ! input )
    {
    cerr << "could not open family.txt\n";
    return 1;
    }
    read_all( input );
    input.close();

    // dump_all( cout );

    // Now allow the user to query the family tree.

    string name;
    while( true )
    {
    cout << "Enter a name, or \"q\" to quit.\n";
    cin >> name;
    if( ! cin )
    break;
    if( name == "q" )
    break;

    CPerson * person = find_person( name );
    if( person == 0 )
    {
    cout << "No such person " << name << "\n";
    }
    else
    {
    dump_person( cout, person );
    }
    }

    return 0;
    }

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Use \t when outputting to the file. It will tab for you. I think that is what u wanted??


    To everyone else: Since this is hw, this person has to use vectors. But wouldn't it be better to use a binary tree??

  3. #3
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    someone should post a sticky, or in the FAQ somewhere that everyone who posts code should do it in either [C O D E] [/C O D E] or in [Q U O T E] [/Q U O T E]

    it makes it much easier to read the code because its indented and the text is smaller
    Paro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 08:09 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21