Hi everyone, I've been trying to figure out how to store values from a .txt file delimited with semicolons (;) into a class which is then stored into a Binary Search Tree. After browsing Google for a few hours, and trying various examples of people using Vectors, I just can't seem to get my program to work using Object Oriented Programming with an instance of the class Person.
My two classes are Person, and BinarySearchTree as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class Person{
private:
string first_surname;
string second_surname;
string name;
int ID;
int telephone;
int score;
public:
Person();
Person(string first_surname, string second_surname, string name, int ID, int telephone, int score);
string getFirstSurname();
string getSecondSurname();
string getName();
int getID();
int getTelephone();
int getScore();
void setFirstSurname(string newfirstsurname);
void setSecondSurname(string newsecondsurname);
void setName(string newname);
void setID(int newid);
void setTelephone(int newtelephone);
void setScore(int newscore);
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
class BinarySearchTree
{
private:
struct tree_node
{
tree_node* left;
tree_node* right;
Person data;
};
tree_node* root;
public:
BinarySearchTree()
{
root = NULL;
}
bool isEmpty() const { return root==NULL; }
void insert(Person);
void search(int IDnumber);
};
|
Ok so my text file saves the data of each person in the same order as the class with each value separated by a semicolon.
i.e. First_Surname;Second_Surname;Name;ID;Telephone;Score;
;smith;jones;david;1016800444;9130303;50
;jacob;sampson;andrew;10163543838;3958837;48
;wilson;villa;sophy;10175593309;4127947;39
;velez;perez;anderson;10002225534;2222323;47
; |
Now the tricky part seems to be in the filling of the tree. I don't know how to manipulate this subprogram so that each value gets stored in a node and then gets sent to the tree.
Here is one example that I have tried, but as it's a vector I get an error that says
no matching function for call to 'Person::setTelephone(std::string&)' etc.
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
|
void fillTree( BinarySearchTree *b)
{
string input[7];
Person p;
fstream file("scores.txt", ios::in); // reads text file
if(file.is_open())
{
getline(file, input[0], ';');
cout<<input[0]<<endl;
while(file.good())
{
getline(file, input[1], ';');
p.setFirstSurname(input[1]);
getline(file, input[2], ';');
p.setSecondSurname(input[2]);
getline(file, input[3], ';');
p.setName(input[3]);
getline(file, input[4], ';');
p.setID(input[4]);
getline(file, input[5], ';');
p.setTelefono(input[5]);
getline(file, input[6], ';');
p.setScore(input[6]);
cout<<p.getFirstSurname()<<", "<<p.getSecondSurname()<<", "<<p.getName()<<", "<<p.getID()<<", "<<p.getTelephone()<<", "<<p.getScore()<<endl;
(*b).insert(p);
}
file.close();
}else{
cout<<"ERROR: File could not be opened. "<<endl;
}
}
|
I understand that I get an error because a vector is saved as integers, and I am using strings, my question is, does anyone know any other way to read the .txt file and save each data separated by a semicolon, into the Person class?
Thanks for taking the time to read this, any help would be kindly appreciated!