As I said, I didn't try my code. Indeed I defined the operator<< inside the OrderTree class, and it then passes this as the first parameter, which is not what I wanted. So here is another attempt... Tell me if that's working for you ?
main.cpp
//////////////////////////////////////////////////////////////
//////main.cpp///
/////////////////////////////////////////////////////////////
//////Author:xxxxxxxxxxxxxx
/////////////////////////////////////////////////////////////
//////Student number:xxxxxxxxx
/////////////////////////////////////////////////////////////
//////Date of creation:July 14,2011
/////////////////////////////////////////////////////////////
#include "Tree.h"
#include <iostream>
#include <string>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
using std::cout;
using std::cin;
OrderTree tree;
void load() //inserting words from a file into the tree.
{
string filename;
//the variable of type ifstream:
std::cout << "Please enter the desired filename with it's extension:\t ";
std::cin >> filename;
const char *file=filename.c_str();
std::ifstream myfile(file, std::ios::in);
if (!myfile.is_open())
{
cout << "Can't open file\n";
}
else if (myfile.is_open())
{
string line;
// read one line at a time
while (getline(myfile, line))
{
stringstream sstrm(line);
std::string English;
std::string Klingon;
if(getline(sstrm, English,':'))
{
sstrm >> Klingon;
cout <<English<<":"<< Klingon <<endl;
// two words extracted
tree.insert(Order(English, Klingon));
}
}
}
}
void insert() //inserting a word into the tree.
{
std::cout <<
"Insert a word into the tree\n"
"Enter a Word:\n <English> : <Klingon> ";
std::string English;
std::string Klingon;
std::cin >> English;
// std::cout << "";
std::cin >> Klingon;
tree.insert(Order(English, Klingon));
cout <<English<<":"<< Klingon <<endl;
}
void remove() //removing a word
{
std::cout << "Remove a word" << std::endl;
std::cout << "Enter word: ";
std::string English;
std::cin >> English;
std::cout << tree.remove(English) << std::endl;
}
void search() //searching for a word in the tree
{
std::cout << "Search for a word" << std::endl;
std::cout << "Enter word:";
std::string English;
std::cin >> English;
Order *foo = tree.search(English);
if (foo)
std::cout << *foo << std::endl;
else
std::cout << "word was not founded.";
}
void print() //printing the contents of the tree
{
std::cout << "Print the words" << std::endl;
std::cout << tree;
}
void out()
{
fstream file("example.txt", ios::out); //open for output
file << tree;
file.close();
std::cout << "OUTPUT FILE CREATED - example.txt" << endl;
}
int menu()
{
std::cout <<
"\n1. Insert a word into the translator\n"
"2. Remove a word from translator\n"
"3. Search for a word\n"
"4. Print the words\n"
"5. Load a new file\n"
"6. Save the file\n"
"7. Quit\n"
"Enter menu number: ";
int n;
std::cin >> n;
return n;
}
int main()
{
cout<< "Welcome to English to Klingon translator\n";
load();
bool done = false;
do
{
switch (menu())
{
case 1:
insert(); //inserting a word
break;
case 2:
remove(); //remove a word
break;
case 3:
search(); // search for a word
break;
case 4:
print(); //printing the tree
break;
case 5:
load(); //load data from a file
break;
case 6:
out(); //updating the file
break;
case 7: //exit
done = true;
break;
}
} while (!done);
}
Tree.h
//////////////////////////////////////////////////////////////
//////Tree.h
/////////////////////////////////////////////////////////////
//////Author:xxxxxxxxxxx
/////////////////////////////////////////////////////////////
//////Student number:xxxxxxxxx
/////////////////////////////////////////////////////////////
//////Date of creation:July 14,2011
/////////////////////////////////////////////////////////////
#pragma once
#include "Order.h"
class OrderTree
{
//private members of the class
private:
struct Node // BST as a reference structure made up of nodes
{
Node *left;
Order data;
Node *right;
Node(): left(0), right(0) {}
Node &operator=(const Node &v)
{
left = v.left;
data = v.data;
right = v.right;
return *this;
}
};
Node *root;
void insert_(Node *&tree, const Order &v) // inserting in the binary tree
{
if (tree)
{
if (v < tree->data) // the node's left subtree has values which are less than it .
insert_(tree->left, v);
else
insert_(tree->right, v); // the node's right subtree has values which are greater than or equal to it .
}
else //crating the first element of the tree
{
tree = new Node;
tree -> left = 0;
tree -> right = 0;
tree -> data = v;
}
}
double remove_(Node *&tree, std::string v, bool &remove) // to remove an element from the tree
{
if (tree)
{
if (v > tree -> data.English())
return remove_(tree -> right, v, remove); //going to the right of the tree if the number is greater
else if (v < tree -> data.English())
return remove_(tree -> left, v, remove); // going to the left of the tree if the number is smaller
else //if the node to be deleted has only one child
{
double c=0;
if ((!tree -> left) && (tree -> right)) // if it has only right child
{
Node *foo = tree -> right;
*tree = *(tree -> right);
delete foo;
}
else if ((tree -> left) && (!tree -> right)) // if it has only left child
{
Node *foo = tree -> left;
*tree = *(tree -> left);
delete foo;
}
else if ((!tree -> left) && (!tree -> right)) // if the node to be deleted does not have any children
{
delete tree;
tree = 0;
}
else // if the node to be deleted has 2 children
{
Node **p = &(tree -> right);
Node *m = tree -> right;
while (m -> left)
{
p = &(m -> left);
m = m -> left;
}
tree -> data = m -> data;
*p = m -> right;
delete m;
}
remove = true; // return true if remove was successful
return c;
}
}
else
{
remove = false; // return false if remove was unsuccessful
return 0;
}
}
void removeAll_(Node *tree) // to delete all the nodes from the tree
{
if (tree)
{
removeAll_(tree->left); //deleting the left subtrees
removeAll_(tree->right); //deleting the right subtrees
delete tree;
}
}
Order *search_(Node *tree, std::string English) const // to search the tree for a given contractor English
{
if (tree)
{
if (English == tree->data.English())
return &(tree->data); // return the leaf if the contractor is found
if (English > tree->data.English())
return search_(tree->right, English); // search the right subtree if the English is greater than the node
if (English < tree->data.English())
return search_(tree->left, English); // search the left subtree if the English is lesser than the node
}
return 0; //return 0, if the contractor English is not found
}
void copy_(const Node *tree) //copy
{
if (tree)
{
insert(tree->data);
copy_(tree->left);
copy_(tree->right);
}
}
public:
OrderTree(): root(0) {}
OrderTree(const OrderTree &v) // copy constructor of the class OrderTree
{
copy_(v.root);
}
~OrderTree() // destructor of the class - deletes the dynamically allocated memory
{
removeAll();
}
const Node* getRoot() const {return root;} //returns the root of the Tree
void insert(const Order &v) //inserting a word into the tree.
{
insert_(root, v);
}
double remove(std::string English) //removing the word
{
bool foo;
return remove_(root, English, foo);
}
double removeContractor(std::string English)
{
bool foo;
double c = 0;
do
{
c += remove_(root, English, foo);
} while (foo);
return c;
}
void removeAll() // removing all the work orders of a contractor from the tree.
{
removeAll_(root);
root = 0;
}
Order *search(std::string English) const //searching for the contractor in the tree
{
return search_(root, English);
}
//Print the tree in order of traversal to an output stream
std::ostream& inOrderPrint(const Node *tree, std::ostream& out) const
{
if (tree)
{
inOrderPrint(tree -> left, out);
out << tree -> data;
inOrderPrint(tree -> right, out);
}
return out;
}
};
std::ostream& operator<<(std::ostream& o, const OrderTree& ordtree)
{
ordtree.inOrderPrint(ordtree.getRoot(), o);
return o;
}
Order.h
//////////////////////////////////////////////////////////////
//////Order.h
/////////////////////////////////////////////////////////////
//////Author:xxxxxxxxxxxxx
/////////////////////////////////////////////////////////////
//////Student number:xxxxxxxxxxxx
/////////////////////////////////////////////////////////////
//////Date of creation:July 14,2011
/////////////////////////////////////////////////////////////
#pragma once
#include <string>
#include <iostream>
#include <string>
#include <fstream>
#include <string>
#include <sstream>
class Order
{
// private members of the class
private:
std::string English_;
std::string translation_;
public:
Order(const std::string &English = "", const std::string &translation = ""): English_(English), translation_(translation)
//constructor to take the English
{
//std::cout << "order......" << endl;
}
Order &operator=(const Order &v) //copy constructor
{
English_ = v.English_;
translation_ = v.translation_;
return *this;
}
std::string English() const
{
return English_; // return the English
}
std::string translation() const
{
return translation_; // return translation
}
void settranslation(const std::string &v)
{
translation_ = v;
} // getting an error message here
bool operator==(const Order& v) const { return English_ == v.English_; }
bool operator!=(const Order& v) const { return English_ != v.English_; }
bool operator<(const Order& v) const { return English_ < v.English_; }
bool operator>(const Order& v) const { return English_ > v.English_; }
bool operator<=(const Order& v) const { return English_ <= v.English_; }
bool operator>=(const Order& v) const { return English_ >= v.English_; }
};
std::ostream &operator<<(std::ostream &s, const Order &o) // print
{
s << o.English() << ":" << o.translation() << std::endl;
return s;
}