title: Segmentation fault error due to a while loop
body: <p>I am implementing a small search engine.The information is taken from a file and all searches containing the word are returned.problem is when I am running the search function in while loop,there is a segmentation fault error but when i run it without a while loop,it runs smoothly..I have rechecked the code quite a few times but can't find any problem.Any help would be appreciated.</p>
  
  <pre><code> //searches individual words which are given to it.array has some data in it
  int search_word(string word,string array)
  {
      int counter=0;
      size_t found;
      int i=0;
      for (i=array.find(word,0);i!=string::npos;i=array.find(word,i))
      {
         counter+=1;
         i++;
      }
     return counter;
  }
  
  
  
  //this is sorting(descending) the linked list of results and storing it in an array
  void Rank()
  {
     struct results *go;
     go=head;
     int array[54];
     int l=0;
     while (go-&gt;next!=0)
     {
         array[l]=go-&gt;number;
         array1[l]=go-&gt;result;
         go=go-&gt;next;
         ++l;
     }
     array[l]=go-&gt;number;
     array1[l]=go-&gt;result;
     int temp;
     string temp1;
     for (int k=0;k&lt;l;k++)
     {
         for (int j=(k+1);j&lt;l+1;j++)
         {
            if (array[k]&lt;array[j])
            {
               temp=array[k];
               array[k]=array[j];
               array[j]=temp;
               temp1=array1[k];
               array1[k]=array1[j];
               array1[j]=temp1;
            }
          }                  
      }
      cout&lt;&lt;"Following results were found"&lt;&lt;endl;
      for (int a=0;a&lt;l;++a)
      {
         cout&lt;&lt;"Result no."&lt;&lt;a&lt;&lt;endl;
         cout&lt;&lt;array1[a]&lt;&lt;endl;
         cout&lt;&lt;endl;
      }
   }
  
   //main search function                
   void search(string array[])
   {
      //words like the,a,he,they,would are low rank words
     ifstream fin;
     fin.open("lowRank.txt");
     string lowrank;
     int j=0;
     while (!fin.eof())
     {
         string buffer;
         do
         {
             getline(fin,buffer);
             lowrank+=" ";
             lowrank+=buffer;
             ++j;
         }
         while (!buffer.empty());
     }
     fin.close();
     cout&lt;&lt;"Enter a word or phrase"&lt;&lt;endl;
     //converting the string containing words into seperate strings by tockenisation
     string setofwords[10];
     char phrase[50];
     cin.get(phrase,50);
     int count=0;
     char *pointer= NULL;
     pointer=strtok(phrase," ");
     while (pointer!=NULL)
     {
         char *pointer= NULL;  
         setofwords[count]=pointer;    
         pointer=strtok(NULL," ");
         count+=1; 
         delete [] pointer;
     }      
  
     for (int i=0;i&lt;54;i++)
     {
         int counter=0;int counter1=0;
         //searching for all words one by one in the below loop       
         for (int k=0;k&lt;count;k++)    
         {         
            //searching if word is low rank
             size_t found;
             found=lowrank.find(setofwords[k]);                 
             if (found!=string::npos)
             {
                 counter=search_word(setofwords[k],array[i]);
                 if (counter&gt;0)
                 {                    
                     counter1+=1;
                 }
             }
             else
             {                                        
                 counter=search_word(setofwords[k],array[i]);
                 counter1+=counter;                   
             }        
          } 
       if (counter1&gt;0)
       {
           add_result(array[i],counter1);
       }
     }
     Rank();
   }
  
  int main()
  {
    //reading the data file and copying it into an a linked list 
     ifstream fin;
     fin.open("data.txt");
      while (!fin.eof())
      {      
          string url;
          string data;
          getline(fin,url);
          string buffer;
         do
         {
              getline(fin,buffer);           
              if (!buffer.empty())
              {
                 data=buffer;
              }
         }
         while (!buffer.empty());
         add_node(url,data);
     }
    //copying linked list to an array
    string array[54];
    node *conductor;
    conductor=root;
    for (int i=0;i&lt;54;i++)
    {
        array[i]=conductor-&gt;url+conductor-&gt;data;
        conductor=conductor-&gt;next;
    }                   
  
    fin.close();
  
    while (1)
    {              
  
  
           cout&lt;&lt;"*******************************SEARCH                                  
           ENGINE********************************"&lt;&lt;endl;
          cout&lt;&lt;endl;
          cout&lt;&lt;endl;
          cout&lt;&lt;endl;
          cout&lt;&lt;"1.Press 1 to search a word."&lt;&lt;endl;
        cout&lt;&lt;"5.Press 5 to quit without saving search results"&lt;&lt;endl;
  
       int ans; 
  
        cin&gt;&gt;ans;     
        if (ans==1)
        {     
            search(array);
            search(array);
        }
  
        else if (ans==5)
        {
            quit_without_saving();
        }
        }
  
  system("PAUSE");
  return 0;
  }
  </code></pre>