Help! Stuck in a loop!

This is a discussion on Help! Stuck in a loop! within the C++ Programming forums, part of the General Programming Boards category; Hi! I'm still working on my Binary search tree. I have to add words from a file to the tree ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    11

    Help! Stuck in a loop!

    Hi!
    I'm still working on my Binary search tree. I have to add words from a file to the tree in sibling or child branches. It seems my Addaword function is wokring a little better now, but something is stuck in an infinite loop. It'll read in the words, and repeat the last word over and over until it crashes.
    I've changed a bunch of things to try and find out where, but I've only changed how it crashes. Can anyone see where my problem is? This is due soon, any help is much appreciated!
    Code:
    class Node
    {
        private:
        Node *sibling;//*
        Node *child;//*
        int occurs;
        char letters;
    
        public:
        AddAWord(char word[]);
        Search(char request[]);
        Node(char l) {occurs=0; sibling=0; child=0; letters=l;}             
     };
    
    
     void main(void)
     {
    
    
             Node *root=new Node('-');
             Node *new_node;
             char word[80];
             int len;
             FILE*input;
             char* request[10];
             char str[80];
             int i = 0, k=0, j=0, l=0;           
    			ifstream b_file("abook.txt", ios::in);
    
    
                     b_file>>word;
    
                     for(j=0;j<strlen(word); j++)
                     {
                             word[j] = tolower((int)word[j]);
                                     }
    
    
    
                     //root->AddAWord(word);
    
    
             while( (b_file>>word) )          //b_file.eof()==0 || i<80)
             {                                          
    				//	b_file>>word;
    
    					root->AddAWord(word);
                    for(int j=0; j<strlen(word); j++)
                     {
                                    word[j] = tolower((int)word[j]);
                     }
    
    
    
                     new_node->AddAWord(word);
             }
    
             cout<<"What word would you like to search for: "<<endl;
    
             cin>>str;       
    /*	int x = root->Search(*request);
    
    	if(x<0)
    		cout<<"this word does not exist in the document!"<<endl;
    	else
    		cout<<"your word appeared "<< x <<" times in the document."<<endl;
    */
      }
    Node::AddAWord(char word[])
    {
    	cout<<word<<endl;
    	//int i=0;
      if(letters != word[0])
      {
    	  if(sibling)
    		  sibling->AddAWord(word);
    	else if(sibling == 0)
    		{
    			sibling=new Node(*(word+1));
    		//	i++;
    			sibling->AddAWord(word);
    		}
      }
      else if (letters ==*word+1)
      {
    	  if(word+1 != '\0')
    	  {
    		  if(child==0)
    		  {child->AddAWord(word);}
    		  else 
    		  {
    			  child = new Node(*word);
    			  //i++;
    			  child->AddAWord(word+1);
    		  }
    	  }
    	  else
    		  occurs = occurs +1;
    
      }
      
    }
    
     Node:: Search ( char request[] )
     {
    
             cout<<request<<endl;     
    		 
     if ( letters==*request )
             child->Search(request);
    
      else if ( letters!=*request )
        sibling->Search(request);
    
     else if(sibling == 0)
             return 0;
    
     else
        return 1;
    
    
     }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    nitpicking here... no real solution...

    so it only walks through the string once:
    Code:
    for(j=0;j<strlen(word); j++) //bad
    
    /* this is better */
    
    wordlen=strlen(word);
    for(j=0;j<wordlen;j++)
    what were you trying to do with this?
    Code:
    word[j] = tolower((int)word[j]);
    IMO, the code looks better like this:
    Code:
    if(child==0)
         {child->AddAWord(word);}
    
    /* the curly's are unnecessary for one line for-loops and if statements */
    
    for(j=0;j<wordlen;j++)
         word[j] = tolower((int)word[j]);
    ...
    if(child==0)
         child->AddAWord(word);
    Last edited by major_small; 12-17-2003 at 09:31 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    If it get's stuck on the input loop, try changing the condition.
    Code:
    while(b_file.peek() != EOF)
    {
    //do whatever you need to do
    }
    I also notice you compare a single char with the first char of the search request. Are you doing this on purpose or are you trying to compare the arays like that?
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 02:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 04:39 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21