I am writing a code to make a dictionary using avl balanced tree structure and file handling. the user enters the word and its meaning. The words are stored in the nodes of the tree and the words and their meaning in a text file. when the user enters the word and the meaning it should be stored in the file as:
word " tab " meaning
But the word and meaning are getting written on 2 different lines. Following is my code:
#include <iostream>
#include<fstream>
#include<string.h>
using namespace std;
class node //Create a class for node.
{ public:
char keyword[10];
char meaning[100];
node *lc;
node *rc;
int h;
friend class avl;
};
class avl //Create a class for avl.
{public:
node* root;
avl()
{
root=NULL; //Initialise root to NULL.
}
node *insert(node *root,char[],char[]);
void insert1();
int height(node *);
int bal_fac(node*);
node *LL(node*);
node *LR(node*);
node *RL(node*);
node *RR(node*);
};
void avl:: insert1() //Insert1 function to read file contents.
{
char data[10]=" ";
char meaning[100];
cout<<endl<<"Enter word: ";
cin>>data;
int len=strlen(data);
len+=4;
char line[len];
cout<<endl<<"Enter its meaning: ";
cin.getline(meaning,100,'.');
fstream myfile;
int i;
for (i=0;i<strlen(data);i++)
{
line[i]=data[i];
}
line[i++]='.';
line[i++]='t';
line[i++]='x';
line[i++]='t';
line[i]='\0';
myfile.open(line,ios::out); //To read from file using object myfile.
if(myfile.is_open())
{
myfile<<"data meaning"<<endl;
myfile.close();
myfile.open(line,ios::app);
myfile<<data<<meaning;
root=insert(root,data,meaning);//Insert new node in tree.
myfile.close();
}
else
cout<<"Unable to open file"<<endl;
}
what changes should I make?