I’m trying to write a program for school and don’t know where to begin.
I’m not very good with pointers so I’m have a bit of difficulty.
Code so far(UPDATED):
#include<iostream>
#include<vector>
#include<string>
using namespace std;
struct node
{
string name;
node * parent;
vector<node*> children;
};
int main()
{
vector<node*> dataBase;
node *advisor, *student, *student2;
advisor = new node;
student = new node;
student2 = new node;
cin>>advisor->name>>student->name>>student2->name;
advisor->children.push_back(student);
advisor->children.push_back(student2);
dataBase.push_back(advisor);
for(int i=0; i<dataBase.size(); i++)
{
cout<<dataBase[i]->name<<endl;
for(int j=0; j<dataBase[i]->children.size(); j++)
{
cout<<dataBase[i]->children[i]->name<<endl;
}
}
system("pause");
return 0;
}
What I want to do is get the input of two names and then store it in the database.
For example the first name would always be the advisor and the second is the student.
I know how to do it on paper, just not with code.. so I’m looking for some examples/tips.
Example:
Input:
John Steven
John Barry
John Harold
Output:
Advisor: John
Students:Steven,Barry,Harold
I want my program to take John and put him in the first entry of the vector dataBase and then I want to take Steven Barry and Harold and put them all in the vector children.
Anyways I know I’m supposed to do this as a tree and where the advisor is the parent and the students are the children of the tree.
Any help/suggestions are welcome. Thank you.
EDIT #1: Now I’m just having trouble adding more students to the one advisor.
You are never adding to the vector. A vector needs to have the element added before you can add index it using the
[]operator.Change
to
To print the students, you can access the
childrenvariable inside yourforloop in exactly the same way as you do to print the advisors name.