Hi,
The following is a function inside a class BST (binary search tree) What I trying to do is to read an input file which contains a collection of user ids and passwords and store each pair of id/password into different node of a binary search tree.
The problem is that all the insert(i,p); calls are done after the while loop is finished...so all the nodes of my tree contain the same user id and password which is the last user id and password in the input file.
I think it might be because insert is a recusive function...I don't know I'm not sure please help!
void read()
{
//temporary variables to handle user id and password
char *i,*p;
//initialize user id and password
i=new char[8];
p=new char[7];
//open the input file
ifstream in("userids.dat"
/***********************************************/
//THIS LOOP IS NOT WORKING
//ALL I GET IS THE LAST LINE OF THE INPUT FILE
//WHY ?????????????????
/***********************************************/
//priming read
in.get(i,8);
//loop until we reach the end of file character
while(!in.eof())
{
//ignore all spaces between the user id and password
in.eatwhite();
//get the user password
in.get(p,7);
//go to the next line in the input file
in.ignore(360,'\n');
/*****************************************************/
//UNCOMMENT THE FOLLOWING LINE AND YOU'LL
//SEE THAT I'M READING THE RIGHT INFO
//cout<<"info: '"<<i<<"' '"<<p<<"'"<<endl;
/****************************************************/
//create and insert a new node with a user id and password
root=inserthelp(root,i,p);
//read the next user id from input file
in.get(i,8);
}
//close the input file
in.close();
}//end of add
just in case you guys would like to see the inserthelp function...here it is:
inserthelp(BinNode<Elem>* subroot, const Elem& val, const Elem& p) {
if (subroot == NULL) // Empty tree: create node
{
return (new BinNodePtr<Elem>(val, p, NULL, NULL));
}
if (EEComp::lt(val, subroot->val())) // Insert on left
subroot->setLeft(inserthelp(subroot->left(), val, p));
else subroot->setRight(inserthelp(subroot->right(), val, p));
return subroot; // Return subtree with node inserted
}
The following is a function inside a class BST (binary search tree) What I trying to do is to read an input file which contains a collection of user ids and passwords and store each pair of id/password into different node of a binary search tree.
The problem is that all the insert(i,p); calls are done after the while loop is finished...so all the nodes of my tree contain the same user id and password which is the last user id and password in the input file.
I think it might be because insert is a recusive function...I don't know I'm not sure please help!
void read()
{
//temporary variables to handle user id and password
char *i,*p;
//initialize user id and password
i=new char[8];
p=new char[7];
//open the input file
ifstream in("userids.dat"
/***********************************************/
//THIS LOOP IS NOT WORKING
//ALL I GET IS THE LAST LINE OF THE INPUT FILE
//WHY ?????????????????
/***********************************************/
//priming read
in.get(i,8);
//loop until we reach the end of file character
while(!in.eof())
{
//ignore all spaces between the user id and password
in.eatwhite();
//get the user password
in.get(p,7);
//go to the next line in the input file
in.ignore(360,'\n');
/*****************************************************/
//UNCOMMENT THE FOLLOWING LINE AND YOU'LL
//SEE THAT I'M READING THE RIGHT INFO
//cout<<"info: '"<<i<<"' '"<<p<<"'"<<endl;
/****************************************************/
//create and insert a new node with a user id and password
root=inserthelp(root,i,p);
//read the next user id from input file
in.get(i,8);
}
//close the input file
in.close();
}//end of add
just in case you guys would like to see the inserthelp function...here it is:
inserthelp(BinNode<Elem>* subroot, const Elem& val, const Elem& p) {
if (subroot == NULL) // Empty tree: create node
{
return (new BinNodePtr<Elem>(val, p, NULL, NULL));
}
if (EEComp::lt(val, subroot->val())) // Insert on left
subroot->setLeft(inserthelp(subroot->left(), val, p));
else subroot->setRight(inserthelp(subroot->right(), val, p));
return subroot; // Return subtree with node inserted
}