Hi,
i am trying to make a dictionary which takes an english word and its meaning in spanish in the following format from an input file.
<english>:<spanish>
i also need to add words manually,remove,search,print ,and store new tree in the same file by updating the file.
my up() and load() are not working so cant read words and store them back to the file.
I would appropriate it if you help me.thanks
#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 a word 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 Spanish;
if(sstrm >> English >> Spanish )
{
cout <<English<<":"<< Spanish <<endl;
// two words extracted ...
tree.insert(Order(English, Spanish));
}
}
}
}
void insert() //inserting a word into the tree.
{
std::cout <<
"Insert a word into the tree\n"
"Enter a Word:\n <English> : <Spanish> ";
std::string English;
std::string Spanish;
std::cin >> Spanish;
std::cout << ":";
std::cin >> Spanish;
tree.insert(Order(English, Spanish));
cout <<English<<":"<< Spanish <<endl;
}
void remove() //removing the first work order of a contractor from the tree
{
std::cout << "Remove the first work order for a word from the tree" << std::endl;
std::cout << "Enter word: ";
unsigned English;
std::cin >> English;
std::cout << tree.remove(English) << std::endl;
}
void search() //searching for the contractor in the tree
{
std::cout << "Search for a word" << std::endl;
std::cout << "Enter word: ";
unsigned 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 in sorted order
{
std::cout << "Print the words" << std::endl;
tree.inOrderPrint();
}
void up()
{
fstream file("example.txt", ios::out); //open for output
file<< "Print the words" << std::endl;
tree.inOrderPrint();
file.seekp(6, ios::beg); //move the put cursor to start of "text"
file<<"program for files"<<endl;
file.close();
}
int menu()
{
std::cout <<
"1. Insert a word into the tree\n"
"2. Remove \n"
"3. Search for a word\n"
"4. Print the words\n"
"5. Quit\n"
"6.Load\n"
"Enter menu number: ";
int n;
std::cin >> n;
return n;
}
int main()
{
bool done = false;
do
{
switch (menu())
{
case 1:
insert(); //inserting a work order into the tree
break;
case 2:
remove(); //remove the first work order for a contractor from a tree
break;
case 3:
search(); // search for a contractor
break;
case 4:
print(); //printing the tree
break;
case 5:
done = true;
break;
case 6:
load(); //load data from a file
break;
case 7:
up(); //updating the tree
break;
}
} while (!done);
}