Thread: help in data structure c++ code

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    help in data structure c++ code

    Code:
    #include <stdio.h>
    #include<iostream.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    //#include <dos.h>
    #include <windows.h>
    #define LEFT  1
    #define RIGHT 2
    
    
    class node
    {
     public:
    
     char word[20],meaning[100];
     node *left,*right;
    };
    
    
    
    
    
    
    node *maketree(char[],char[]);
    
    node* treefromfile();
    void filefromtree(node*);
    void addword(node*,char[],char[]);
    void seperateword(char[],char[],char[]);
    void displayall(node*);
    node* bsearch(node*,char[]);
    void showmenu();
    FILE *file_ptr;
    
    
    
    
    
    
    void prog()
    {
    
     char word[20],meaning[100];
     int menuchoice;
     node *temp;
     temp=treefromfile();
     if(temp==NULL)
     {
      cout<<"File does not exist or dictionary is empty...";
      getch();
     }
     while(1)
     {
      
      showmenu();
      cin>>menuchoice;
      switch(menuchoice)
      {
       case 1:cout<<"Enter word ";
    	  cin>>word;
    	  cout<<"Enter meaning : " ;
    	  flushall();
    	  cout.flush();
    	  gets(meaning);
    	  if(temp==NULL)
    	   temp=maketree(word,meaning);
    	  else
    	   addword(temp,word,meaning);
    	  break;
       case 2:if(temp==NULL)
    	   cout<<"The dictionary is empty...";
    	  else
    	  {
    	   cout<<"Find meaning of : ";
    	   flushall();
    	   gets(word);
    	   node *t;
    	   t=bsearch(temp,word);
    	   if(t==NULL)
    	    printf("Word not found...");
    	   else
    	   {
    	    cout<< "t->word";
    	    puts(t->meaning);
    	   }
    	  }
    	  getch();
    	  break;
       case 3:if(temp==NULL)
    	   printf("Dictionary is empty...");
    	  else
    	   displayall(temp);
    	  getch();
    	  break;
       case 4:filefromtree(temp);
    	  exit(1);
    	  break;
       default:cout<<"Enter Again";
    	   Sleep(1000);
    	   prog();
    	   break;
      }
    
    
    
     
     
     }
    
    
    
    
    }
    
    
    
    
    void showmenu()
    {
     cout<<"\t\t\nCOMPUTER DICTIONARY"<<endl;
     cout<<"\t[1].	Add a word."<<endl;
     cout<<"\t[2].	Find meaning."<<endl;
     cout<<"\t[3].	Display all."<<endl;
     cout<<"\t[4]. Save and Close."<<endl;
    
    
    
    //Enter Choice"()";
    }
    node* treefromfile()
    {
     node *ptree=NULL;
     char word[20],meaning[100],str[120],*i;
     int flags=0;
     file_ptr=fopen("c:\\dict.anu","r");
     if(file_ptr==NULL)
      ptree=NULL;
     else
     {
    
      while(!feof(file_ptr))
      {
    	i=fgets(str,120,file_ptr);
    	if(i==NULL)
    	break;
    	seperateword(str,word,meaning);
    	if(flags==0)
    	{
    	 ptree=maketree(word,meaning);
    	 flags=1;
    	}
    	else
    	 addword(ptree,word,meaning);
      }
    
      fclose(file_ptr);
     }
     return ptree;
    }
    
    
    
    
    node* maketree(char w[],char m[])
    {
     node *p;
     p=new node;
     strcpy(p->word,w);
     strcpy(p->meaning,m);
     p->left=NULL;
     p->right=NULL;
     return p;
    }
     
    
    
    void seperateword(char str[],char w[],char m[])
    {
     int i,j;
     for(i=0;str[i]!=' ';i++)
     {
      w[i]=str[i];
      w[i++]=NULL;	//Append the null and skip the space.
     }
     for(j=0;str[i]!='\0';i++,j++)
     {
      m[j]=str[i];
     }
     m[j]=NULL;
     }
    
    
    
    
    
    
    void addword(node *tree,char word[],char meaning[])
    {
     node *p,*q;
     p=q=tree;
     while(strcmp(word,p->word)!=0 && p!=NULL)
     {
      q=p;
      if(strcmp(word,p->word)<0)
       p=p->left;
      else
       p=p->right;
     }
     if(strcmp(word,q->word)==0)
     {
      cout<<"This word already exists...";
      Sleep(1000);
     }
     else if(strcmp(word,q->word)<0)
      q->left=maketree(word,meaning);
     else
      q->right=maketree(word,meaning);
    }
    
    
    
    
    
    
    node* bsearch(node *tree,char word[])
    {
     node *q;
     q=tree;
     while(q!=NULL)
     {
      //p=q;
      if(strcmp(word,q->word)<0)
       q=q->left;
      else if(strcmp(word,q->word)>0)
       q=q->right;
      if(strcmp(word,q->word)==0)
       break;
     }
     return q;
    }
    
    
    
    
    void filefromtree(node *tree)
    {
     void travandwrite(node*);
     file_ptr=fopen("c:\\dict.anu","w");
     if(file_ptr==NULL)
     {
       cout<<"Cannot open file for writing data..."<<endl;
     }
     else //if(tree==NULL)
     {
      if(tree!=NULL)
      {
       travandwrite(tree);
      }
      fclose(file_ptr);  //Close the file anyway.
     }
    }
    void travandwrite(node *tree)
    {
     if(tree!=NULL)
     {
      fprintf(file_ptr,"%s,%s",tree->word,tree->meaning);
      travandwrite(tree->left);
      travandwrite(tree->right);
     }
    }
    void displayall(node *tree)
    {
     if(tree!=NULL)
     {
      displayall(tree->left);
      //cout<<"tree->word,tree->meaning";
      displayall(tree->right);
     }
    }
    
    void intro()
    {
    int i;
    cout<<"DICTIONARY LOADING ..."<<endl;
    for(i=0;i<50;i++)
    {
     cout<<"\tþþþ";
     cout<<2*i<<"% completed"<<endl;
     Sleep(150);
    }
    cout<<"\t\tDICTIONARY LOADING COMPLETED"<<endl;
    
    
    }
    void main()
    {
    
    intro();
    prog();
    }


    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////




    this my code it is not workin properly plzzzz help me out plzzzzz
    Last edited by Salem; 01-30-2011 at 11:05 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    please use code tags..then I'll (or others) might read it
    You ended that sentence with a preposition...Bastard!

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also, be more specific about "not workin properly" -- compile errors? run-time crashes? wrong output?

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    code is crash on run time

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1. What compiler/IDE are you using (MSVC 6?), I see some old headers being used.

    #2. Your code is not very C++ish. You use a mix of C and C++ I/O which should be avoided. You use character arrays when you should be using std::string objects instead. Your use of gets in particular is worrying.

    #3.
    Code:
    void main()
    It's int main, never void main.

    #4.
    Code:
    void intro()
    {
        int i;
        cout<<"DICTIONARY LOADING ..."<<endl;
        for(i=0;i<50;i++)
        {
            cout<<"\tþþþ";
            cout<<2*i<<"% completed"<<endl;
            Sleep(150);
        }
        cout<<"\t\tDICTIONARY LOADING COMPLETED"<<endl;
    }
    This function serves no purpose other than wasting the users time for 150*50 milliseconds (7.5 seconds).

    #5.
    Code:
    void prog()
    {
        ...
        while(1)
        {
            ...
            switch(menuchoice)
            {
            ...
            default:cout<<"Enter Again";
                Sleep(1000);
                prog();
                break;
            }
        }
    }
    There's no good reason to be recursively calling your function here. Amongst other things, it results in calling the treefromfile function all over again which reloads your tree from scratch.

    #6.
    Code:
    node* treefromfile()
    {
        node *ptree=NULL;
        char word[20],meaning[100],str[120],*i;
        int flags=0;
        file_ptr=fopen("c:\\dict.anu","r");
        if(file_ptr==NULL)
            ptree=NULL;
        else
        {
            while(!feof(file_ptr))
            {
                i=fgets(str,120,file_ptr);
    Do not use feof to control you loops. Instead, test the result of the read operation for success/failure and go from there.

    #7. I see you using operator new to dynamically allocate memory... good. Where is the call to delete to clean up your allocated memory. You've got memory leakage.

    #8.
    Quote Originally Posted by caca
    code is crash on run time
    Where in particular does it crash? When doing a specific option?




    Haven't checked any further than that but fixing those should be a good start to making this program look and work better.
    Last edited by hk_mp5kpdw; 01-31-2011 at 07:50 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Looks like typical Turbo Crap++ code to me.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    plzzzzzzzz plzzzzzzz plzzzzz correct my code it is a dictionary using tree when i run my code it gona crashesd plzz correct it and mail me here my email id
    <<<snipped>>>
    thanks

  8. #8
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by caca View Post
    plzzzzzzzz plzzzzzzz plzzzzz correct my code it is a dictionary using tree when i run my code it gona crashesd plzz correct it and mail me here my email id
    <<<snipped>>>
    thanks

    Where does this entitlement come from? You got very good suggestions, do the fixes YOURSELF. If you want people to do it for you, go look for a freelance. Be ready to pay for it.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    We don't fix code. We fix the programmer so that he/she can write correct code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 12:23 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  4. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM