It's been a week until I have written a code in C++ which implements a dictionary program. It reads from my "dictionary.txt" file and stores it in a linked list
My linked list node is:
struct node {
string data;
string m;
int mcount ;
struct node *link;
};
Search() Function is
int search ( char *str )
{
struct node *head = NULL ;
node *newnode;
string word = str;
node *p;
newnode = new node();
char temp1 [ 20 ] ;
char temp2 [ 20 ] ;
int i ;
ifstream n;
//n = dic [ toupper ( str [ 0 ] ) - 65 ] ;
n.open("dictionary.txt", ios::out);
if(n.fail()){
cout<<"\nFile not found!";
exit(0);
}
strcpy ( temp2, str ) ;
strupr ( temp2 ) ;
head = newnode;
while ( !n.eof() )
{
getline(n, newnode->data , ' ');
newnode->link = new node();
newnode = newnode->link;
}
p = head;
p->link=newnode;
node *tempnode;
tempnode = head;
while(tempnode!=NULL){
cout<< tempnode->data;
tempnode = tempnode->link;
}
I want it to work like this: If I want to search a word in the dictionary.txt file, it should get its meaning from the dictionary.txt file. But it's not getting data on basis of my input, It's just getting the same output every time. In short, it's not making a search according to my input.
I have stored meanings of words like this in "dictionary.txt"
alive living
again repeat
against opposite
ant name of insect
It should search on the basis of the first word until it encounters whitespace or some special character.
Main()
int main() {
string word;
int i;
cout<< "\nEnter the word to search : " ;
fflush ( stdin );
gets ( word ) ;
i = search ( word ) ;
if ( ! i )
cout<< "Word does not exists." ;
return 0;
}
Is it something I am missing or what is the error? I have read too many articles but many less or no replies match my problem.