0

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.

AlexG
  • 59
  • 2
  • 9
  • 2
    `gets ( word ) ;` How is this `gets` defined? – MikeCAT Jul 08 '21 at 10:55
  • 1
    Your usage of `while ( !n.eof() )` is [wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). You should check if reading succeeded before using what is "read". Also note that `fflush(stdin);` [invokes *undefined behavior*](https://stackoverflow.com/questions/2979209/using-fflushstdin). – MikeCAT Jul 08 '21 at 10:56
  • 2
    Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) including all of required headers and declarations. – MikeCAT Jul 08 '21 at 10:59
  • there is no `return` in the code for `search` – 463035818_is_not_an_ai Jul 08 '21 at 11:00
  • @463035818_is_not_a_number It looks like the function `search` is not disclosed until the end (there is no `}` to indicate the end of function body) Again, a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) is required. – MikeCAT Jul 08 '21 at 11:05
  • Here are the library files I am using! #include #include #include #include #include #include #include #include – AlexG Jul 08 '21 at 11:37
  • Look like there is an issue in getline() function... But I am not an expert to resolve it. – AlexG Jul 08 '21 at 11:41

0 Answers0